首页 > Java > java教程 > 正文

如何使用Apache POI将二维数组数据导出到Excel表格?

聖光之護
发布: 2024-11-30 19:45:02
原创
1092人浏览过

如何使用apache poi将二维数组数据导出到excel表格?

将二维数组导出为 excel

为了将二维数组写入 excel 文件,可以使用 apache poi 库。以下步骤展示如何实现:

引入 maven 依赖

<dependency>
    <groupid>org.apache.poi</groupid>
    <artifactid>poi</artifactid>
    <version>3.17</version>
</dependency>
<dependency>
    <groupid>org.apache.poi</groupid>
    <artifactid>poi-ooxml</artifactid>
    <version>3.17</version>
</dependency>
登录后复制

创建工作簿和工作表

xssfworkbook workbook = new xssfworkbook();
xssfsheet sheet = workbook.createsheet("worksheet");
登录后复制

创建表头

酷表ChatExcel
酷表ChatExcel

北大团队开发的通过聊天来操作Excel表格的AI工具

酷表ChatExcel 48
查看详情 酷表ChatExcel
xssfrow row = sheet.createrow(0);
for (int i = 0; i < headlist.size(); i++) {
    xssfcell cell = row.createcell(i);
    cell.setcellvalue(headlist.get(i));
}
登录后复制

写入数据

for (int i = 0; i < contentlist.size(); i++) {
    row = sheet.createrow(i + 1);
    for (int j = 0; j < contentlist.get(i).size(); j++) {
        row.createcell(j).setcellvalue(contentlist.get(i).get(j));
    }
}
登录后复制

保存文件

xssfworkbook workbook = new xssfworkbook();
workbook.write(new fileoutputstream(file));
workbook.close();
登录后复制

示例代码

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WriteToExcel {

    public static void main(String[] args) {
        // 表头测试数据
        List<String> headList = new ArrayList<>();
        headList.add("昵称");
        headList.add("年龄");

        // 内容测试数据
        List<List<String>> contentList = getContent();

        // 创建工作簿和工作表
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("WorkSheet");

        // 设置表头
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headList.size(); i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue(headList.get(i));
        }

        // 设置内容
        for (int i = 0; i < contentList.size(); i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < contentList.get(i).size(); j++) {
                row.createCell(j).setCellValue(contentList.get(i).get(j));
            }
        }

        // 保存文件
        try {
            workbook.write(new FileOutputStream("D://work.xls"));
            workbook.close();
            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected static List<List<String>> getContent() {
        List<List<String>> contentList = new ArrayList<>();
        List<String> content1 = new ArrayList<>();
        content1.add("张三");
        content1.add("18");
        List<String> content2 = new ArrayList<>();
        content2.add("李四");
        content2.add("20");
        contentList.add(content1);
        contentList.add(content2);
        return contentList;
    }
}
登录后复制

以上就是如何使用Apache POI将二维数组数据导出到Excel表格?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号