POI和easyExcel详解

POI-Excel写数据

03版本

java 复制代码
public void testWrite03() throws Exception {
        //创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet("大憨表 ");
        //创建一个行(1,1)
        Row row1 = sheet.createRow(0);
        //创建一个单元格
        Cell cell = row1.createCell(0);
        cell.setCellValue("大憨1号");
        //(1,2)
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue("大憨2号");
        //第二行(2,1)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);
        //生成一张表(IO流) 03版本使用的xls结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATh + "大憨统计表03.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("大憨统计表生成完毕");
    }

07版本

java 复制代码
public void testWrite07() throws Exception {
        //创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet("大憨表");
        //创建一个行(1,1)
        Row row1 = sheet.createRow(0);
        //创建一个单元格
        Cell cell = row1.createCell(0);
        cell.setCellValue("大憨1号");
        //(1,2)
        Cell cell2 = row1.createCell(1);
        cell2.setCellValue("大憨2号");
        //第二行(2,1)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        //(2,2)
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);
        //生成一张表(IO流) 07版本使用的xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATh + "大憨统计表04.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("大憨统计表生成完毕");
    }

区别

对象的区别 以及文件后缀

批量数据导入

大文件写HSSF

缺点:最多处理65536行,否则会抛出异常

优点:过程中写入缓存,不操作磁盘,最后一次性写入磁盘,速度快

java 复制代码
public void testWrite03BigData() throws Exception {
    //时间
    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 + "testWrite03Bigfile.xls");
    workbook.write(fileOutputStream);
    fileOutputStream.close();
    long end = System.currentTimeMillis();
    System.out.println((double)(end-begin)/1000);
}

大文件写XSSF

缺点:写数据时速度非常慢,非常消耗内存,也会发生内存溢出,如100万条

优点:可以写较大的数据量,如20万条

java 复制代码
public void testWrite07BigData() throws Exception {
    //时间
    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 + "testWrite07Bigfile.xlsx");
    workbook.write(fileOutputStream);
    fileOutputStream.close();
    long end = System.currentTimeMillis();
    System.out.println((double)(end-begin)/1000);
}

大文件写SXSSF

优点:可以写非常大的数据量,如果100万条甚至更多条,写数据速度快,占用更少内存

注意

过程中会产生临时文件,需要清理临时文件

默认由100条记录被保存在内存中,如果超过这数量,则最前面的数据呗写入临时文件

如果想自定义内存中数据的数量,可以使用new SXSSFWorkbook

java 复制代码
public void testWrite07BigDataS() throws Exception {
    //时间
    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读数据

java 复制代码
public void testRead03() throws Exception{
​
    //获取文件流
    FileInputStream fileInputStream = new FileInputStream(PATH + "dahanpoi大憨统计表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.getStringCellValue());
    fileInputStream.close();
​
​
}

读取不同的数据类型

java 复制代码
public void testCellType() throws Exception{
    FileInputStream fileInputStream = new FileInputStream(PATH + "");
​
    Workbook workbook = new HSSFWorkbook(fileInputStream);
    Sheet sheetAt = workbook.getSheetAt(0);
    //获取标题内容
    Row row = sheetAt.getRow(0);
    if (row!=null){
        int cellCount = row.getPhysicalNumberOfCells();
        for (int cellNum = 0; cellNum < cellCount; cellNum++) {
            Cell cell = row.getCell(cellNum);
            if (cell!=null){
                int cellType = cell.getCellType();
                String stringCellValue = cell.getStringCellValue();
                System.out.println(stringCellValue+" | " );
            }
        }
    }
    //获取表中内容
    int rowCount = sheetAt.getPhysicalNumberOfRows();
    for (int rowNum = 1; rowNum < rowCount; rowNum++) {
        Row row1 = sheetAt.getRow(rowNum);
        if (row1!=null){
            int cellCount = row1.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
​
                Cell cell = row1.getCell(cellNum);
                if (cell!=null){
                    int cellType = cell.getCellType();
                    String cellValue = "";
                    switch (cellType){
                        case HSSFCell
                                .CELL_TYPE_STRING://字符串
                            System.out.println("String");
                            cellValue = cell.getStringCellValue();
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN://布尔
                            System.out.println("BOOLEAN");
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case HSSFCell.CELL_TYPE_BLANK://空
                            System.out.println("空");
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case HSSFCell.CELL_TYPE_NUMERIC:
                            System.out.println("NUMERIC");
                            if (HSSFDateUtil.isCellDateFormatted(cell)){//日期
                                System.out.println("日期");
                                Date date = cell.getDateCellValue();
                                cellValue = new DateTime(date).toString("yyyy-MM-dd");
                            }else {
                                //不是日期格式 防止数字过长
                                System.out.println("转换为字符串输出");
                                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                cellValue = cell.toString();
                            }
                            break;
                        case HSSFCell.CELL_TYPE_ERROR:
                            System.out.println("数据类型错误");
                            break;
                    }
                    System.out.println(cellValue);
                }
​
            }
        }
    }
    fileInputStream.close();
}

计算公式

java 复制代码
public void testFormula() throws Exception{
    FileInputStream fileInputStream = new FileInputStream(PATH + "公式.xls");
    Workbook workbook = new HSSFWorkbook(fileInputStream);
    Sheet sheetAt = workbook.getSheetAt(0);
    Row row = sheetAt.getRow(4);
    Cell cell = row.getCell(0);
    //拿到计算公式
    FormulaEvaluator FormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
    //输出单元格的内容
    int cellType = cell.getCellType();
    switch (cellType){
        case Cell.CELL_TYPE_FORMULA://公式 
            String cellFormula = cell.getCellFormula();
            System.out.println(cellFormula);
            
            //计算
            CellValue evaluate = FormulaEvaluator.evaluate(cell);
            String cellValue = evaluate.formatAsString();
            System.out.println(cellValue);
            break;
    }
​
}

EasyExcel

导入依赖

java 复制代码
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.6</version>
        </dependency>
相关推荐
Rain的Java大神之路8 分钟前
Spring Boot 4 把自动配置拆成 47 个模块后,我的 `dependency:tree` 翻天了:一份从排查到治理的完整实战手册
java·spring boot·后端
xurileidian2 小时前
第 17 篇 · 边玩边学Python · for 遍历 enumerate 与 zip
java·前端·python
Mikko73 小时前
Redis 主从切换期间可用吗?6.2 实测哨兵切换:应用不可写 33 秒,主从连不上、数据不一致,以及设了 ACL 后哨兵不切换
java·运维·redis·后端
不得了zz3 小时前
11-Agent 可观测与评估:Spring AI Alibaba Admin Studio 到底解决了什么
java·人工智能·后端·spring·ai编程·springaialibaba
软件技术NINI3 小时前
基于 SpringBoot 的酒店预定系统
java·spring boot·后端
田里的水稻4 小时前
FA_排序算法-冒泡排序
java·算法·排序算法
会编程的吕洞宾5 小时前
Spring Cloud Gateway 限流鉴权与灰度发布实战
java·经验分享·后端·spring
AI开源软件7 小时前
SpringCloud+Nacos 业务协同落地:业务调用与集成中心远程管理如何分别配置
java·spring boot
sharemylove17 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
FYKJ_201017 小时前
springboot众创项目团队管理系统94666-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spark·课程设计