欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Apache POI操作Excel写入大量数据

程序员文章站 2022-04-12 22:47:51
创建一个maven项目引入相关依赖 org.apache.poi poi 3.17 org.apache.poi
  1. 创建一个maven项目
  2. 引入相关依赖
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
</dependency>
  1. 采用HSSF方式

缺点:通过HSSF方式写入大量数据,最多只能处理65536行,超过65536行会报错。

测试一下:

@Test
public void hssfTest() throws IOException {
    String PATH = "E:\\code\\javaSE\\";
    long start = System.currentTimeMillis();
    //创建一个工作簿
    Workbook workbook = new HSSFWorkbook();
    //创建一个表
    Sheet sheet = workbook.createSheet("xls03版本");
    for (int rowNum = 0; rowNum < 65537; rowNum++) {
        //创建行
        Row row = sheet.createRow(rowNum);
        for (int cellRow = 0; cellRow < 10; cellRow++) {
            //创建列
            Cell cell = row.createCell(cellRow);
            cell.setCellValue(cellRow);
        }
    }
    System.out.println("over");
    //生产一张表(IO流) 03版本的用xls结尾
    FileOutputStream fileOutputStream = new FileOutputStream(PATH + "PoiHSSF01.xls");
    workbook.write(fileOutputStream);
    fileOutputStream.close();
    long end = System.currentTimeMillis();
    System.out.println((double) (end - start)/1000);
}

报错:java.lang.IllegalArgumentException: Invalid row number (65536) outside allowable range (0..65535)

优点:通过HSSF方式写入65536行数据,消耗的时间很短,处理过程全部写入缓存,不操作磁盘,最后一次性写入磁盘,速度很快。

@Test
public void hssfTest() throws IOException {
    String PATH = "E:\\code\\javaSE\\";
    long start = System.currentTimeMillis();
    //创建一个工作簿
    Workbook workbook = new HSSFWorkbook();
    //创建一个表
    Sheet sheet = workbook.createSheet("03xls");
    for (int rowNum = 0; rowNum < 65536; rowNum++) {
        //创建行
        Row row = sheet.createRow(rowNum);
        for (int cellRow = 0; cellRow < 10; cellRow++) {
            //创建列
            Cell cell = row.createCell(cellRow);
            cell.setCellValue(cellRow);
        }
    }
    System.out.println("over");
    //生产一张表(IO流) 03版本的用xls结尾
    FileOutputStream fileOutputStream = new FileOutputStream(PATH + "PoiHSSF01.xls");
    workbook.write(fileOutputStream);
    fileOutputStream.close();
    long end = System.currentTimeMillis();
    System.out.println((double) (end - start)/1000);
}
over
2.163

Apache POI操作Excel写入大量数据

  1. 采用XSSF方式

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

@Test
public void xssfTest() throws IOException {
    String PATH = "E:\\code\\javaSE\\";
    long start = System.currentTimeMillis();
    //创建一个工作簿
    Workbook workbook = new XSSFWorkbook();
    //创建一个表
    Sheet sheet = workbook.createSheet("07xlsx");
    for (int rowNum = 0; rowNum < 200000; rowNum++) {
        //创建行
        Row row = sheet.createRow(rowNum);
        for (int cellRow = 0; cellRow < 10; cellRow++) {
            //创建列
            Cell cell = row.createCell(cellRow);
            cell.setCellValue(cellRow);
        }
    }
    System.out.println("over");
    //生产一张表(IO流) 03版本的用xlsx结尾
    FileOutputStream fileOutputStream = new FileOutputStream(PATH + "PoiHSSF01.xlsx");
    workbook.write(fileOutputStream);
    fileOutputStream.close();
    long end = System.currentTimeMillis();
    System.out.println((double) (end - start)/1000);
}
over
22.504

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

Apache POI操作Excel写入大量数据

  1. 采用SXSSF方式写入数据
@Test
public void sxssfTest() throws IOException {
    String PATH = "E:\\code\\javaSE\\";
    long start = System.currentTimeMillis();
    //创建一个工作簿
    Workbook workbook = new SXSSFWorkbook();
    //创建一个表
    Sheet sheet = workbook.createSheet("07sxlsx");
    for (int rowNum = 0; rowNum < 200000; rowNum++) {
        //创建行
        Row row = sheet.createRow(rowNum);
        for (int cellRow = 0; cellRow < 10; cellRow++) {
            //创建列
            Cell cell = row.createCell(cellRow);
            cell.setCellValue(cellRow);
        }
    }
    System.out.println("over");
    //生产一张表(IO流) 07版本的用xlsx结尾
    FileOutputStream fileOutputStream = new FileOutputStream(PATH + "PoiHSSF02.xlsx");
    workbook.write(fileOutputStream);
    fileOutputStream.close();
    //清除临时文件
    ((SXSSFWorkbook) workbook).dispose();
    long end = System.currentTimeMillis();
    System.out.println((double) (end - start)/1000);
}
over
3.867

优点:可以写入非常大量数据时,速度很快,占用内存少

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

本文地址:https://blog.csdn.net/jiang_wang01/article/details/109623893

相关标签: java excel