POI:对Excel的基本写操作 整理1

首先导入相关依赖 

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <!--xls(03)-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>5.2.2</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <!--xlsx(07)-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>5.2.2</version>
    </dependency>

    <!-- 日期格式化工具 -->
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.12.5</version>
    </dependency>

    <!--测试-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>

1 写入简单的数据

public class ExcelWrite {

    String PATH = "D:\\Idea-projects\\POI\\POI_projects";

    // 以下是相应的写入操作方法
    // ......
}

(1)当向03版本的Excel中写入数据

    @Test
    public void testWrite03() throws IOException {
//        1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook();

//        2.创建一个工作表
        Sheet sheet = workbook.createSheet("xx");

//        3.创建一行
        Row row = sheet.createRow(0);
//        4.创建一个单元格
        Cell cell1 = row.createCell(0);
        cell1.setCellValue("今日新增观众");

        Cell cell2 = row.createCell(1);
        cell2.setCellValue(666);


//        创建第二行(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);


//        5. 生成一张表 (IO 流)   03版本就是使用xls结尾

        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xx03.xls");

        workbook.write(fileOutputStream);

//        6.关闭流
        fileOutputStream.close();

        System.out.println("文件生成完毕!");
    }

(2)当向07版本的Excel中写入数据

    @Test
    public void testWrite07() throws IOException {
//        1.创建一个工作簿
        Workbook workbook = new XSSFWorkbook();

//        2.创建一个工作表
        Sheet sheet = workbook.createSheet("xiexu1");

//        3.创建一行
        Row row = sheet.createRow(0);
//        4.创建一个单元格
        Cell cell1 = row.createCell(0);
        cell1.setCellValue("今日新增观众");

        Cell cell2 = row.createCell(1);
        cell2.setCellValue(666);


//        创建第二行(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);


//        5. 生成一张表(IO 流)   03版本就是使用xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "xiexu07.xlsx");

        workbook.write(fileOutputStream);

//        6.关闭流
        fileOutputStream.close();

        System.out.println("文件生成完毕!");

    }

2 写入大量的数据

(1)当向03版本的Excel中写入大量数据

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

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

    @Test
    public void testWriteBigData03() throws IOException {
        // 计算时间差
        long beginTime = System.currentTimeMillis();
        // 1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook();

        // 2.创建一个工作表
        Sheet sheet = workbook.createSheet("xiexu2");

        // 3. 写入数据  (03版最多只能放65536行, 超出会报异常)
        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("写入完成!");
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "bigData03.xls");
        workbook.write(fileOutputStream);

        fileOutputStream.close();

        long endTime = System.currentTimeMillis();

        System.out.println((double) (endTime - beginTime) / 1000);  // 毫秒转换为秒
    }


1.129s  还是非常快的


只能写入65536行,超出会报异常


(2)当向07版本的Excel中写入大量数据

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

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

    @Test
    public void testWriteBigData07() throws IOException {   // 耗时长
        // 计算时间差
        long beginTime = System.currentTimeMillis();
        // 1.创建一个工作簿
        Workbook workbook = new XSSFWorkbook();

        // 2.创建一个工作表
        Sheet sheet = workbook.createSheet("xiexu3");

        // 3. 写入数据
        for (int rowNum = 0 ; rowNum < 65538; rowNum++) {   // ? 65538
            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 + "bigData07.xlsx");
        workbook.write(fileOutputStream);

        fileOutputStream.close();

        long endTime = System.currentTimeMillis();
        System.out.println((double) (endTime - beginTime) / 1000);  // 毫秒转换为秒
    }

耗时4.812s, 相比于03版本,是非常慢的


可以写入超出65536行的数据 

(3)因为07版本写入大量数据时的速度较慢,所以我们可以用07的加速版的做法:SXSSFWorkbork()

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

注意:过程中会产生临时文件,需要清理临时文件(默认有100条记录被保存在内存中,如果超出了这个数量,则最前面的数据被写入临时文件,如果想自定义内存中数据的数量,可以使用  new  SXSSFWorkbook(自定义的数量值)  ) 

    @Test
    public void testWriteBigData07quick() throws IOException {   // SXSSF 更快
        // 计算时间差
        long beginTime = System.currentTimeMillis();
        // 1.创建一个工作簿
        Workbook workbook = new SXSSFWorkbook();

        // 2.创建一个工作表
        Sheet sheet = workbook.createSheet("xiexu4");

        // 3. 写入数据
        for (int rowNum = 0 ; rowNum < 65538; rowNum++) {   // ? 65538
            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 + "bigData07quick.xlsx");
        workbook.write(fileOutputStream);

        fileOutputStream.close();
        ((SXSSFWorkbook) workbook).dispose();    // 清除临时文件
        long endTime = System.currentTimeMillis();
        System.out.println((double) (endTime - beginTime) / 1000);  // 毫秒转换为秒
    }


现在是1.667s相比于之前,还是非常快的

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小蟹呀^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值