记着层次   
//记着层次  excel ->sheet  ->row ->cell

package com.sky.config;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class POITest {
    public static void main(String[] args) throws IOException {
//        write();
        read();
    }

    private static void read() throws IOException { //记着层次  excel ->sheet  ->row ->cell
        FileInputStream fileInputStream = new FileInputStream(new File("D://temp//one.xlsx") );
        XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
//        XSSFSheet sheet = sheets.getSheet("第一次创建sheet");
        XSSFSheet sheet = sheets.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            String rawValue = row.getCell(0).getStringCellValue();
            String rawValue1 = row.getCell(1).getStringCellValue();
            System.out.println(rawValue +" : "+ rawValue1);


        }

        fileInputStream.close();
        sheets.close();
    }

    private static void write() throws IOException {
        XSSFWorkbook excel = new XSSFWorkbook();
        XSSFSheet sheet = excel.createSheet("第一次创建sheet");
        XSSFRow row = sheet.createRow(0);//第一行是标题
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("年级");
        for (int i = 1; i <=100 ; i++) {

            row = sheet.createRow(i);//第二行
            row.createCell(0).setCellValue("美丽小"+i);
            row.createCell(1).setCellValue("18"+":"+i);
        }
//         row = sheet.createRow(1);//第二行
//        row.createCell(0).setCellValue("美丽小");
//        row.createCell(1).setCellValue("18");
//
//         row = sheet.createRow(2);//第三行
//        row.createCell(0).setCellValue("美丽华");
//        row.createCell(1).setCellValue("81");

        FileOutputStream fileOutputStream = new FileOutputStream(new File("D://temp//one.xlsx"));

        excel.write(fileOutputStream);
        fileOutputStream.close();
        excel.close();
    }
}

  

 

 
posted on 2023-09-12 19:50  mljqqh  阅读(7)  评论(0编辑  收藏  举报