POI及EasyExcel(Java 操作 Excel)

POI及EasyExcel

1.什么是POI

Apache POI官网
在这里插入图片描述在这里插入图片描述
2.什么是EasyExcel

easyExcel官网
在这里插入图片描述EasyExcel是阿里巴巴开源的一个excel处理框架,以简单使用、节省内存著称。
在这里插入图片描述easyexcel官方文档

POI存在内存问题:100w条数据先加载到内存中,可能会产生OOM异常。
EasyExcel:是从磁盘一行一行的返回。

POI-Excel写

创建项目

  1. 建立一个普通的Maven项目
  2. 引入pom依赖
<dependencies>
    <!--    excel03-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    <!--    excel07-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>
<!--    日期格式化工具-->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>
  1. 03版本和07版本
    工作薄:工作表:行:单元格
    private static final String PATH="C:\\Users\\hzy\\Desktop\\程序员之窝\\POI及EasyExcel\\pol-easyexcel";

03版本

@Test
    public void testWrite03() throws IOException {
//        1.创建一个工作薄
        Workbook workbook=new HSSFWorkbook();
//        2.创建一个工作表
        Sheet sheet=workbook.createSheet("统计表");
//        3.创建一个行(1,1)
        Row row1=sheet.createRow(0);
//        4.创建一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日新增");
//        (1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);
//        第二行
        Row row2=sheet.createRow(1);
//        (2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
//        (2,2)
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
//        生成一张表(.xls)
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "统计表03.xls");
//        输出
        workbook.write(fileOutputStream);
//        关闭流
        fileOutputStream.close();
        System.out.println("统计表03  生成完毕!");
    }

07版本

@Test
    public void testWrite07() throws IOException {
//        1.创建一个工作薄
        Workbook workbook=new XSSFWorkbook();
//        2.创建一个工作表
        Sheet sheet=workbook.createSheet("统计表");
//        3.创建一个行(1,1)
        Row row1=sheet.createRow(0);
//        4.创建一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("今日新增");
//        (1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue(666);
//        第二行
        Row row2=sheet.createRow(1);
//        (2,1)
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
//        (2,2)
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
//        生成一张表(.xlsx)
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "统计表07.xlsx");
//        输出
        workbook.write(fileOutputStream);
//        关闭流
        fileOutputStream.close();
        System.out.println("统计表07  生成完毕!");
    }

注意对象的一个区别,文件后缀!
数据批量导入!

大文件写HSSF

缺点:最多只能处理65536行,否则会抛出异常
优点:过程中写入缓存,不操作磁盘,最后一次性写入磁盘,速度快

 @Test
    public void testWrite03BigData() throws IOException {
//        时间差
        long begin = System.currentTimeMillis();
//        创建一个薄
        Workbook workbook=new HSSFWorkbook();
//        创建表
        Sheet sheet = workbook.createSheet();
//        写入数据
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell=row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("OVER");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite03BigData.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end=System.currentTimeMillis();
        System.out.println((double) (end-begin)/1000);
    }

大文件写XSSF

缺点:写数据时速度非常慢,非常耗内存,也会发生内存溢出,如100万条
优点:可以写较大的数据量,如20万条

//    耗时较长!优化,缓存
    @Test
    public void testWrite07BigData() throws IOException {
//        时间差
        long begin = System.currentTimeMillis();
//        创建一个薄
        Workbook workbook=new XSSFWorkbook();
//        创建表
        Sheet sheet = workbook.createSheet();
//        写入数据
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell=row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("OVER");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite07BigData.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end=System.currentTimeMillis();
        System.out.println((double) (end-begin)/1000);
    }

大文件写SXSSF

优点:可以写非常大的数据量,如100万条甚至更多条,写数据速度快,占用更少的内存
注意:
过程中会产生临时文件,需要清理临时文件
默认由100条记录被保存在内存中,如果超过这数量,则最前面的数据被写入临时文件
如果想自定义内存中数据的数量,可以使用new SXSSFWWorkbook(数量)

@Test
    public void testWrite07BigDataS() throws IOException {
//        时间差
        long begin = System.currentTimeMillis();
//        创建一个薄
        Workbook workbook=new SXSSFWorkbook();
//        创建表
        Sheet sheet = workbook.createSheet();
//        写入数据
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell=row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("OVER");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "testWrite07BigDataS.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
//        清除临时文件
        ((SXSSFWorkbook)workbook).dispose();
        long end=System.currentTimeMillis();
        System.out.println((double) (end-begin)/1000);
    }

在这里插入图片描述

POI-Excel读

03

    private static final String PATH="C:\\Users\\hzy\\Desktop\\程序员之窝\\POI及EasyExcel\\pol-easyexcel";
    @Test
    public void testWrite03() throws IOException {
//        1.创建一个工作薄,使用Excel能操作的这边都可以操作
//        获取文件流
        FileInputStream fileInputStream = new FileInputStream(PATH + "统计表03.xls");
        Workbook workbook = new HSSFWorkbook(fileInputStream);
//        得到表
        Sheet sheetAt = workbook.getSheetAt(0);
//        得到行
        Row row = sheetAt.getRow(0);
//        得到单元格
        Cell cell = row.getCell(0);
        System.out.println(cell.getNumericCellValue());
        fileInputStream.close();
    }

07同理,更改工作薄类型,文件后缀名(注意类型问题)。

读取不同的数据类型

  1. 封装将单元格类型统一成字符串
//    封装将单元格类型统一成字符串
    private String getCellValue(Cell cell,int cellType){
        String cellValue="";
        switch (cellType){
            case HSSFCell.CELL_TYPE_STRING: //字符串
                System.out.print("[CELL_TYPE_STRING]");
                cellValue = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                System.out.print("[CELL_TYPE_BOOLEAN]");
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                System.out.print("[CELL_TYPE_BLANK]");
                break;
            case HSSFCell.CELL_TYPE_NUMERIC://数字(日期、普通数字)
                System.out.print("[CELL_TYPE_NUMERIC]");
                if(HSSFDateUtil.isCellDateFormatted(cell)){//日期
                    System.out.print("[日期]");
                    Date date = cell.getDateCellValue();
                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                }else{
                    //如果不是日期过期,防止数字过长
                    System.out.print("[普通数字]");
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cellValue = cell.toString();
                }
                break;
            case HSSFCell.CELL_TYPE_ERROR:
                System.out.print("[CELL_TYPE_ERROR]");
                break;
        }
        return cellValue;
    }
  1. 使用封装方法输出表格所有数据
@Test
    public void testCellType() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(PATH + "统计表07.xlsx");
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        //获取标题内容
        Row rowTitle = sheet.getRow(0);
        if(rowTitle!=null){
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount;   cellNum++) {
                Cell cell = rowTitle.getCell(cellNum);
                if(cell!=null){
                    int cellType = cell.getCellType();
                    String cellValue=getCellValue(cell,cellType);
                    System.out.print(cellValue+" | ");
                }
            }
            System.out.println();
        }
        //获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            if(rowData!=null){
                //读取列
                int cellCount = rowTitle.getPhysicalNumberOfCells();
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    System.out.print("["+(rowNum+1)+"-"+(cellNum+1)+"]");
                    Cell cell = rowData.getCell(cellNum);
                    //匹配列的数据类型
                    if(cell!=null){
                        int cellType = cell.getCellType();
                        String cellValue=getCellValue(cell,cellType);
                        System.out.println(cellValue);
                    }

                }
            }
        }
        fileInputStream.close();
    }

注意数据转换问题!

可以将两个方法封装成一个工具类,只需要向testCellType()方法传入new FileInputStream(PATH + "统计表07.xlsx");即可!

计算公式

@Test
    public void testFormula() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(PATH + "统计表03.xls");
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(2);

        //拿到计算公式 eval
        FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook)workbook);
        //输出单元格的内容
        int cellType = cell.getCellType();
        switch (cellType){
            case Cell.CELL_TYPE_FORMULA: //公式
                String formula=cell.getCellFormula();
                System.out.println(formula);
                //计算
                CellValue evaluate = formulaEvaluator.evaluate(cell);
                System.out.println(evaluate.formatAsString());
                break;
        }

    }

EasyExcel操作

快速,简单避免OOM的java处理Excel工具
pom

<dependency>
  <groupId>com.alibaba</groupId>
   <artifactId>easyexcel</artifactId>
   <version>2.2.0-beta2</version>
</dependency>
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.18.12</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.60</version>
</dependency>

官网的例子很详细!
easyexcel官方文档
固定套路:

  1. 写入,固定类格式进行写入!
  2. 读取,根据监听器设置的规则进行读取!

小结-学习方式

测试EasyExcel所有API!
easyExcel官网
easyexcel官方文档
了解,面向对象的思想,学会面向接口的编程!
理解使用测试API!

发布了21 篇原创文章 · 获赞 7 · 访问量 435

猜你喜欢

转载自blog.csdn.net/weixin_42998267/article/details/105704303