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);

    }
}
相关推荐
Gent_倪21 小时前
数据治理之安全管理:Apache Ranger
apache
SelectDB2 天前
Apache Doris 实战教程:手把手实现 ClickHouse 表结构迁移与数据校验
apache
SelectDB2 天前
Apache Doris 实战教程:从零搭建 MCP Server,让 AI Agent 直接用自然语言查数据
apache
SelectDB2 天前
Apache Doris Segment V3 宽表优化实战教程:从建表到体验元数据按需加载
apache
java_logo2 天前
Docker Compose 部署 Apache Superset:轻松搭建开源 BI 平台
docker·开源·apache·superset·轩辕镜像·superset部署方案·docker superset
Gent_倪3 天前
数据治理之元数据管理:Apache Atlas
apache
Kina_C3 天前
Apache HTTP Server 安装、配置与高级功能详解
linux·http·apache
中北marry3 天前
玄机靶场wp 第一章 日志分析-Apache日志分析
apache
java_logo3 天前
Apache Doris Docker 部署指南:实时分析数据库实战
数据库·docker·apache·doris·apache doris·轩辕镜像·docker部署doris
Zhu7584 天前
在k8s环境部署Apache Superset最新版
容器·kubernetes·apache