使用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();
    }
}

效果展示

相关推荐
LAM LAB6 天前
【VBA】Excel指定单元格范围内字体设置样式,处理导出课表单元格
excel·vba
在这habit之下6 天前
Keepalived学习总结
excel
james的分享6 天前
大数据领域核心 SQL 优化框架Apache Calcite介绍
大数据·sql·apache·calcite
莫寒清6 天前
Apache Tika
java·人工智能·spring·apache·知识图谱
Youngchatgpt6 天前
如何在 Excel 中使用 ChatGPT:自动化任务和编写公式
人工智能·chatgpt·自动化·excel
开开心心就好6 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
D_C_tyu6 天前
Vue3 + Element Plus | el-table 多级表头表格导出 Excel(含合并单元格、单元格居中)第二版
vue.js·elementui·excel
归叶再无青6 天前
web服务安装部署、性能升级等(Apache、Nginx)
运维·前端·nginx·云原生·apache·bash
骆驼爱记录6 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发