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>
相关推荐
lybugproducer1 小时前
创建型设计模式之:简单工厂模式、工厂方法模式、抽象工厂模式、建造者模式和原型模式
java·设计模式·建造者模式·简单工厂模式·工厂方法模式·抽象工厂模式·面向对象
南客先生1 小时前
马架构的Netty、MQTT、CoAP面试之旅
java·mqtt·面试·netty·coap
Minyy111 小时前
SpringBoot程序的创建以及特点,配置文件,LogBack记录日志,配置过滤器、拦截器、全局异常
xml·java·spring boot·后端·spring·mybatis·logback
百锦再1 小时前
Java与Kotlin在Android开发中的全面对比分析
android·java·google·kotlin·app·效率·趋势
武昌库里写JAVA2 小时前
39.剖析无处不在的数据结构
java·vue.js·spring boot·课程设计·宠物管理
Nelson_hehe5 小时前
Java基础第四章、面向对象
java·语法基础·面向对象程序设计
Thomas_YXQ5 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
ShiinaMashirol6 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
东阳马生架构7 小时前
Nacos简介—3.Nacos的配置简介
java
北极的企鹅887 小时前
XML内容解析成实体类
xml·java·开发语言