使用Apache POI将数据写入Excel文件

首先导入依赖

复制代码
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>

其次写个测试类

复制代码
public class ExcelTest {
    /**
     * 基于POI向Excel文件写入数据
     * @throws Exception
     */
    public static void write() throws Exception{
        //在内存中创建一个Excel文件对象
        XSSFWorkbook excel = new XSSFWorkbook();
        //创建Sheet页
        XSSFSheet sheet = excel.createSheet("rejiao");

        //在Sheet页中创建行,0表示第1行
        XSSFRow row1 = sheet.createRow(0);

        //创建单元格并在单元格中设置值,单元格编号也是从0开始,1表示第2个单元格
        row1.createCell(0).setCellValue("pushId");

        for (int i = 0; i < 9999; i++) {
            XSSFRow row2 = sheet.createRow(i+1);
            row2.createCell(0).setCellValue("push" + (10000 + i));
        }



        FileOutputStream out = new FileOutputStream(new File("D:\\rejiao.xlsx"));
        //通过输出流将内存中的Excel文件写入到磁盘上
        excel.write(out);

        //关闭资源
        out.flush();
        out.close();
        excel.close();
    }
    public static void main(String[] args) throws Exception {
        write();
    }
}

效果展示

相关推荐
ratel️11 小时前
小墨析 · Office Agent 日常办公不烧心
python·langchain·word·excel·fastapi·js·ppt
Logic10113 小时前
没有TextSplit函数的老版本也能拆分单元格的内容
excel·拆分数据·excel技巧·数据拆分·拆分单元格·textsplit·旧版excel
2601_9679873714 小时前
你们都用Python实现了哪些自动化办公?
python·excel·自动化办公·1024程序员节·学习资料·跨平台操作
HoneyMoose14 小时前
Ubuntu Apache 配置多站点提示 SSL 错误
ubuntu·apache·ssl
骑着蜗牛撵大象32715 小时前
一套引擎撑起树表双模型:Apache IoTDB 2.0 与 TsFile 的选型答卷
apache·时序数据库·iotdb·tsfile·树模型·表模型
城数派15 小时前
1901-2025年中国省/市/县三级逐月平均气温数据Excel+Shp格式
excel
凤山老林16 小时前
Spring Boot 集成 Apache CXF 发布与消费 SOAP WebService:WSDL、WS-Security
spring boot·后端·apache·webservice·soap·apache cxf
inquisiter2 天前
KernelGpt的部分尝试
java·前端·apache
gtstore2 天前
从 Excel 迁移到仓库管理系统的数据准备与切换方案
excel·数据迁移·仓库管理系统·进销存·信息化实施
十碗饭吃不饱2 天前
Apache POI XWPFTableCell 表格单元格排版踩坑总结
apache