Apache POI练习代码

目录

    • pom.xml
    • [ExcelWriteTest .java](#ExcelWriteTest .java)

pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.smy</groupId>
    <artifactId>smy-poi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--导入依赖-->
    <dependencies>
        <!-- Java 万物皆对象 -->
        <!--xls(03版本)-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <!--xlsx(07版本)-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <!--日期格式化工具-->
        <!-- 比Java官方的utils.date好用 -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

ExcelWriteTest .java

java 复制代码
package com.smy;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Description:POI写测试
 * Author:smy
 * Date: 2024/12/17 17:19
 */
public class ExcelWriteTest {
    //copy absolute path 后不要忘记最后要加双斜杠,否则路径不对
    String PATH = "D:\\practise\\PoiExcelTestSuite\\smy-poi\\";

    @Test
    public void testWrite03() throws IOException {
        //1、创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //2、创建一个工作表(工作簿中包含工作表,通过工作簿来创建工作表)
        Sheet sheet = workbook.createSheet("sheetTest1");//默认为缺省值,可以创建名称
        //3、创建一个行
        Row row1 = sheet.createRow(0);
        //4、创建一个单元格(1,1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("smy111");
        //(1,2)
        Cell cell12 = row1.createCell(1);
        //bool类型
        cell12.setCellValue(true);

        //第二行
        Row row2 = sheet.createRow(1);
        //(2,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操作,IO流)  03 版本使用xls结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表03.xls");

        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        System.out.println("观众统计表03 生成完毕!");
    }

    @Test
    public void testWrite07() throws IOException {
        //1、创建一个工作簿 07版本,与03仅对象不同,面向接口编程的好处,对象变了,接口不用变,代码不用改
        //!注意对象的区别以及文件后缀!
        Workbook workbook = new XSSFWorkbook();
        //2、创建一个工作表(工作簿中包含工作表,通过工作簿来创建工作表)
        Sheet sheet = workbook.createSheet("sheetTest1");//默认为缺省值,可以创建名称
        //3、创建一个行
        Row row1 = sheet.createRow(0);
        //4、创建一个单元格(1,1)
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("第一行第一个单元格值为smy111");
        //(1,2)
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("第一行第二个单元格值为smy222");
        //(1,5)
        Cell cell15 = row1.createCell(4);
        cell15.setCellValue("测试布尔类型");


        //第二行
        Row row2 = sheet.createRow(1);
        //(2,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);

        //(2,5)
        Cell cell25 = row2.createCell(4);
        cell25.setCellType(XSSFCell.CELL_TYPE_BOOLEAN);
        cell25.setCellValue(true);

        //生成一张表(本质是IO操作,IO流)  07 版本使用xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表07.xlsx");

        //输出
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();

        System.out.println("观众统计表07 生成完毕!");
    }

    @Test
    public void testWrite03BigData() throws IOException {
        //记录时间差
        long beginTime = System.currentTimeMillis();
        System.out.println("开始时间:" + beginTime);

        //创建一个工作簿
        Workbook workbook = new HSSFWorkbook();

        //创建一个工作表
        Sheet sheet = workbook.createSheet("smy测试表1");

        //循环创建行(03版本 行数范围:0~65535)
        //缺点:最多只能处理65536行,否则会抛出异常:java.lang.IllegalArgumentException: Invalid row number (65536) outside allowable range (0..65535)
        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 endTime = System.currentTimeMillis();
        System.out.println("结束时间:" + endTime);
        //可能有小数,所以转成double类型,除以一千以秒形式展示
        System.out.println("生成excel表所需要的时间:" + (double)(endTime - beginTime)/1000);
    }


    //耗时较长!优化:考虑使用缓存---->XSSF---->SXSSF
    @Test
    public void testWrite07BigData() throws IOException {
        //记录时间差
        long beginTime = System.currentTimeMillis();
        System.out.println("开始时间:" + beginTime);

        //创建一个工作簿
        Workbook workbook = new XSSFWorkbook();

        //创建一个工作表
        Sheet sheet = workbook.createSheet("smy测试表1");

        //循环创建行(07版本 行数不限,但是同样的65565耗时比03版本的长很多)
        for (int rowNum = 0; rowNum < 100000; 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 endTime = System.currentTimeMillis();
        System.out.println("结束时间:" + endTime);
        //可能有小数,所以转成double类型,除以一千以秒形式展示
        System.out.println("生成excel表所需要的时间:" + (double)(endTime - beginTime)/1000);


        //问:POI能否加速?     SXSSF
    }


    //使用缓存来加快速度
    @Test
    public void testWrite07BigDataS() throws IOException {
        //记录时间差
        long beginTime = System.currentTimeMillis();
        System.out.println("开始时间:" + beginTime);

        //创建一个工作簿,S:super
        Workbook workbook = new SXSSFWorkbook();

        //创建一个工作表
        Sheet sheet = workbook.createSheet("smy测试表1");

        //循环创建行(十万条数据,使用SXSSF比XSSF快多了)
        for (int rowNum = 0; rowNum < 100000; 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 endTime = System.currentTimeMillis();
        System.out.println("结束时间:" + endTime);
        //可能有小数,所以转成double类型,除以一千以秒形式展示
        System.out.println("生成excel表所需要的时间:" + (double)(endTime - beginTime)/1000);

    }
}
相关推荐
ps酷教程37 分钟前
Apache httpclient & okhttp(2)
okhttp·apache
沙子可可5 小时前
Apache Camel指南-第四章:路由径构建之异常处理
apache·集成学习
程序猿熊跃晖6 小时前
Excel 数据导入与 SQL 生成:基于 Hutool 和 Apache POI 的优雅实践
sql·apache·excel
SeaTunnel2 天前
Apache SeaTunnel 2.3.10 正式发布 —— 全新功能与多项改进,助力数据集成再升级!
apache
路由侠内网穿透2 天前
本地部署开源流处理框架 Apache Flink 并实现外部访问
大数据·网络协议·tcp/ip·flink·服务发现·apache·consul
故事与他6453 天前
TBKDVR硬盘录像机device.rsp命令执行漏洞
服务器·网络·数据库·安全·网络安全·apache
DDDiccc5 天前
项目-苍穹外卖(十七) Apache POI+导出数据
apache
智慧源点5 天前
Apache Doris 高频问题排查指南:从报错到性能优化
apache
摇滚侠6 天前
org.apache.maven.surefire:surefire-junit-platform:jar:2.22.2 Maven打包失败
junit·maven·apache
爱穿衬衫的张某某6 天前
httpClient_apache过滤ssl证书校验
网络协议·apache·ssl