POI操作EXCLE(一行一行的读取或者写入数据)

maven需要的配置



   org.apache.poi
   poi
   3.17


   org.apache.poi
   poi-ooxml
   3.17


   org.apache.poi
   poi-ooxml-schemas
   3.17


   junit
   junit
   compile


读取EXCLE 


import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.*;

//读取EXCLE
public class POITest {
    private static void readExcel(String str) throws FileNotFoundException, IOException {
        File file = new File(str);
        // HSSFWorkbook 2003的excel .xls,XSSFWorkbook导入2007的excel   .xlsx
//      HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(new File(file)));
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(file));
        Sheet sheet = workbook.getSheetAt(0);//读取第一个 sheet
        List list = new ArrayList<>();
        Row row = null;
        int count = sheet.getPhysicalNumberOfRows();
        //逐行处理 excel 数据
        for (int i = 0; i < count; i++) {
            JSONObject json = new JSONObject();
            row = sheet.getRow(i);
            if (row != null) {
                Cell cell0 = row.getCell(0);
                //设置取值为String
                //整数数据要转,否则会变成浮点数
                Cell cell1 = row.getCell(1);
                Cell cell2 = row.getCell(2);
                Cell cell3 = row.getCell(3);
                json.put("Id", cell0.toString()); //编号
                json.put("Name", cell1.toString()); //名称
                if(cell2!= null){
                    json.put("Title", cell2.toString()); //名称
                }
                if(cell3!= null){
                    json.put("Type", cell3.toString()); //名称
                }
                list.add(json);
                System.out.println("json:" + json);
            }
        }
        workbook.close();
        System.out.println("list:" + list);
    }


    public static void main(String[] args) throws IOException {
        readExcel("C:\\Users\\Administrator\\Desktop\\模板(1).xlsx");
}

写入数据到EXCLE 


import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
//写入数据到EXCLE
public class ExcelUtil {

    /**
     * 导出Excel
     *
     * @param sheetName sheet名称
     * @param title     标题
     * @param values    内容
     * @param wb        HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }

    public static void main(String[] args) throws IOException {
        //excel标题
        String[] title = {"名称", "性别", "年龄", "学校", "班级"};
        //excel文件名
        String fileName = "学生信息表" + System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "学生信息表";
        String[][] content = null;
        for (int i = 0; i < 2; i++) {
            content = new String[title.length][5];
            content[i][0] = "张三";
            content[i][1] = "男";
            content[i][2] = "21";
            content[i][3] = "国际学校";
            content[i][4] = "一年级一班";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        FileOutputStream output = new FileOutputStream("D:\\web\\" + fileName);
        wb.write(output);
        output.flush();
    }
}

你可能感兴趣的:(技术开发)