树形结构导出excel表格

树形结构导出excel表格

在这里插入图片描述
原本是想做成这样
在这里插入图片描述
但是没学会,最后做成这样
在这里插入图片描述

直接看代码

public String excelDowmload(DormCheckStatParamDto param)  {
        String resultUrl = null;
        param.setSearchAll(SEARCH_ALL_NO);
        try {
            DormCheckResultDto dormCheckResultDto = CheckRecordTime(param);//这里就是要导出的结构集,我这里的结果集是个树形结构
            resultUrl = exportTreeStructureWithDataToExcel(dormCheckResultDto);
            System.out.println("文件已上传,访问链接: " + resultUrl);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DataException de) {
            LOG.info("打印日志:{}",JSON.toJSONString(de));
        }
        return resultUrl;
    }
public  String exportTreeStructureWithDataToExcel(DormCheckResultDto root) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("宿舍检查结果树形结构");
        // 写入表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("部门名称");
        headerRow.createCell(1).setCellValue("类型");
        headerRow.createCell(2).setCellValue("在寝人数");
        headerRow.createCell(3).setCellValue("晚归人数");
        headerRow.createCell(4).setCellValue("请假外宿人数");
        headerRow.createCell(5).setCellValue("缺寝人数");
        headerRow.createCell(6).setCellValue("未查寝人数");
        headerRow.createCell(7).setCellValue("离寝人数");
        headerRow.createCell(8).setCellValue("滞留人数");
        headerRow.createCell(9).setCellValue("请假在寝人数");
        exportNodeWithData(sheet, root, 1,0); // 开始导出,根节点从第二行开始,第一行为表头
        try {
            String uuid = CoreUtils.getUUID();
            File tmpFile = File.createTempFile(uuid, ".xlsx");

            try (OutputStream outputStream = new FileOutputStream(tmpFile)) {
                workbook.write(outputStream);
                outputStream.flush();
            }
            try (FileInputStream inputStream = new FileInputStream(tmpFile)) {
                long fileSize = tmpFile.length();
                //这里我是需要上传到公司服务器,也可以做成到客户端下载
                String resultUrl = fileClient.put(inputStream, uuid + ".xlsx", fileSize, "dorm", null, null);
                if (CoreUtils.isNull(resultUrl)) {
                    throw new DataException("上传文件有误");
                }
                return resultUrl; // 返回上传后的文件URL
            }
        } finally {
            workbook.close(); // 确保工作簿资源被释放
        }
    }
private static int recursionCount =1;//这里定义一个全局变量是为了定位数据导出到excel的行数,这是最简单的方法,也可以通过在递归中返回rowIndex定位行数
    private  void exportNodeWithData(Sheet sheet, DormCheckResultDto node, int rowIndex,int spacing) {
        recursionCount++;
        Row row = sheet.createRow(rowIndex);
        Cell cell = row.createCell(0);
        cell.setCellValue(node.getDeptName());
        cell.setCellStyle(createCellStyle(sheet.getWorkbook(), spacing)); // 根据需要调整缩进
        //这里根据缩进增加了一个类型
        if (spacing == 0) {
            row.createCell(1).setCellValue("校区");
        }else if (spacing == 1) {
            row.createCell(1).setCellValue("院系");
        }else if (spacing == 2) {
            row.createCell(1).setCellValue("专业");
        }else if (spacing == 3) {
            row.createCell(1).setCellValue("班级");
        }
        //这里记得做判断
        row.createCell(2).setCellValue(node.getDormCheckResultIn());
        row.createCell(3).setCellValue(node.getDormCheckResultLate());
        row.createCell(4).setCellValue(node.getDormCheckResultApplyLeave());
        row.createCell(5).setCellValue(node.getDormCheckResultAbsent());
        row.createCell(6).setCellValue(node.getDormCheckResultInit());
        row.createCell(7).setCellValue(node.getDormCheckResultLeft());
        row.createCell(8).setCellValue(node.getDormCheckResultStay());
        row.createCell(9).setCellValue(node.getDormCheckResultApplyStay());
        // 递归处理子节点
        if (node.getDormCheckResultInDtos() != null) {
            spacing++;
            for (int i = 0; i < node.getDormCheckResultInDtos().size(); i++) {
                exportNodeWithData(sheet, node.getDormCheckResultInDtos().get(i), recursionCount,spacing); // 调整下一行的索引
            }
        }
    }
private  CellStyle createCellStyle(Workbook workbook, int indentLevel) {
        CellStyle style = workbook.createCellStyle();
        style.setIndention((short) (indentLevel * 4)); // 设置缩进,自己选择长度
        return style;
    }

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树形结构数据导出Excel 可以使用 Java 的 POI 库来实现。POI 是一组用于处理 Microsoft Office 文档的 Java 库,包括 Excel、Word、PowerPoint 等文档格式。 具体实现如下: 1. 在 pom.xml 中添加以下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建 Excel 文件并写入数据。这里以树形结构数据为例,假设已经从数据库中查询到了树形结构数据,可以使用递归的方式将数据写入 Excel 表格中。 ```java public void exportToExcel(List<Node> nodeList, String filePath) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(); int rowIndex = 0; for (Node node : nodeList) { rowIndex = writeNodeToExcel(sheet, node, rowIndex, 0); } FileOutputStream outputStream = new FileOutputStream(filePath); workbook.write(outputStream); workbook.close(); } private int writeNodeToExcel(Sheet sheet, Node node, int rowIndex, int level) { Row row = sheet.createRow(rowIndex++); Cell cell = row.createCell(level); cell.setCellValue(node.getName()); for (Node child : node.getChildren()) { rowIndex = writeNodeToExcel(sheet, child, rowIndex, level + 1); } return rowIndex; } ``` 以上代码中,`Node` 表示树形结构中的一个节点,包含了节点名称和子节点列表。`exportToExcel` 方法接收一个节点列表和一个文件路径作为参数,将节点列表写入 Excel 文件中。`writeNodeToExcel` 方法递归地将节点和子节点写入 Excel 表格中,其中 `level` 参数表示节点的层级。 3. 调用 `exportToExcel` 方法将数据导出Excel 文件。 ```java List<Node> nodeList = // 从数据库中查询树形结构数据 String filePath = "/path/to/excel.xlsx"; exportToExcel(nodeList, filePath); ``` 以上就是将树形结构数据导出Excel 的方法,代码中省略了异常处理等细节,请根据实际情况进行补充。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值