EasyExcel 多sheet页导出

 public void export() {
            Workbook workbook = new XSSFWorkbook();
            // 查询数据data 导出标头信息可以用as转换成中文名称
            exportToExcelSheet(workbook,data,"sheet名称");
            // 多次调用 exportToExcelSheet
        // 将工作簿写入文件
        try (FileOutputStream fileOut = new FileOutputStream("D://WorkBook.xlsx")) {
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭工作簿
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

/**
 * 组装sheet页数据
**/ 
public static void exportToExcelSheet(Workbook workbook, List<JSONObject> data, String sheetName) {
        
        Sheet sheet = workbook.createSheet(sheetName);
        // 如果数据不为空
        if (!data.isEmpty()) {
            // 获取第一个JSON对象的所有键,作为表头
            JSONObject firstObject = data.get(0);
            Row headerRow = sheet.createRow(0);
            int columnCount = 0;
            for (String key : firstObject.keySet()) {
                Cell cell = headerRow.createCell(columnCount++);
                cell.setCellValue(key);
            }
            // 遍历数据列表,为每行数据创建一行
            int rowCount = 1;
            for (JSONObject jsonObject : data) {
                Row row = sheet.createRow(rowCount++);
                columnCount = 0;
                for (String key : firstObject.keySet()) { // 使用相同的键顺序
                    Cell cell = row.createCell(columnCount++);
                    cell.setCellValue(jsonObject.getStr(key, ""));
                }
            }
        }


    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容