欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 树型结构递归导出excel,合并父级单元格

树型结构递归导出excel,合并父级单元格

2025/5/6 23:12:06 来源:https://blog.csdn.net/qq_36363499/article/details/144909729  浏览:    关键词:树型结构递归导出excel,合并父级单元格

树型结构递归导出excel,合并父级单元格

最近遇到一个需求,需要将树型结构列表数据导致至excel表格中,并且要合并父级数据的单元格,类似下图
在这里插入图片描述
通过递归算法真实导出来之后,发现子级节点会有空行,空行原因是父级写在了上一下,子级写在下一级,来呈现一个树结构,类似下图
在这里插入图片描述
因此需要调整递归算法逻辑,判断是叶子节点时,才创建新的一行,否则不创建新行,获取到的数据写在同一行,单元格合并逻辑是遍历到叶子节点列表最后一个时,则合并上一级父节点单元格

完整代码如下

在这里插入代码片
```public void exportWorkCodeExcel(HttpServletResponse response) throws IOException {List<WhProject> permissionDataList = whProjectService.getProjectPermissionDataList().stream().sorted(Comparator.comparing(WhProject::getLevel).reversed()).collect(Collectors.toList());Integer maxLevel = permissionDataList.stream().findFirst().get().getLevel();List<ProjectTreeVo> projectTreeVos = buildTreeUseStream(permissionDataList, 0L);String[] CHINESE_DIGITS = {"二", "三", "四", "五", "六", "七", "八", "九"};String workCodeHeadTitle = "%s级工时编码";String nameHeadTitle = "%s级工时编码名称";//创建工作薄XSSFWorkbook wb = new XSSFWorkbook();//创建工作表XSSFSheet sheet = wb.createSheet("工时编码");//设置样式XSSFCellStyle style = wb.createCellStyle();//水平对齐style.setAlignment(HorizontalAlignment.CENTER);//垂直对齐style.setVerticalAlignment(VerticalAlignment.CENTER);XSSFFont font = wb.createFont();font.setBold(true);style.setFont(font);//表头//冻结表头sheet.createFreezePane(0, 1);XSSFRow sheet1row1 = sheet.createRow((short) 0);sheet1row1.setHeight((short) 480);//写入表头int circle = 0;for(int i = 0,j = 1; i < (maxLevel-1); i++,j = j + 1) {String columnCode = String.format(workCodeHeadTitle,CHINESE_DIGITS[i]);String columnName = String.format(nameHeadTitle, CHINESE_DIGITS[i]);//列XSSFCell cellCode = sheet1row1.createCell(i + circle);XSSFCell cellName = sheet1row1.createCell(j + circle);cellCode.setCellValue(columnCode);cellCode.setCellStyle(style);cellName.setCellValue(columnName);cellName.setCellStyle(style);circle = circle + 1;}//数据开始行int rowIndex = 1;for (ProjectTreeVo projectTreeVo : projectTreeVos) {rowIndex = writeExcel(sheet, projectTreeVo, rowIndex, 0);}response.setContentType("application/x-download");response.setCharacterEncoding("UTF-8");String outFileName = URLEncoder.encode("工时编码.xlsx", "UTF-8");//修复中文文件名输出乱码response.setHeader("Content-disposition", "attachment;filename=" + outFileName + ";" + "filename*=utf-8''" + outFileName);// 写入输出流OutputStream outputStream = response.getOutputStream();wb.write(outputStream);outputStream.flush();outputStream.close();wb.close();}/*** 写数据到excel中* @param sheet  表格* @param projectTreeVo  当前节点* @param rowIndex 行号* @param colIndex 列号*/private int writeExcel(XSSFSheet sheet, ProjectTreeVo projectTreeVo, int rowIndex, int colIndex) {int startRow = rowIndex;Row row = sheet.getRow(rowIndex);if(Objects.isNull(row)){row = sheet.createRow(rowIndex);row.setHeight((short) 380);}Cell cell1 = row.createCell(colIndex);cell1.setCellValue(projectTreeVo.getWorkHourCode());//设置单元格宽度sheet.setColumnWidth(colIndex, 256 * 25);colIndex = colIndex + 1;Cell cell2 = row.createCell(colIndex);cell2.setCellValue(projectTreeVo.getName());//自适应单元格宽度//sheet.autoSizeColumn(colIndex);sheet.setColumnWidth(colIndex, 256 * 25);for (ProjectTreeVo child : projectTreeVo.getChildren()) {rowIndex = writeExcel(sheet, child, rowIndex, colIndex + 1);}int endRow = rowIndex - 1;if (startRow < endRow) { // 如果有子节点,则合并父级单元格sheet.addMergedRegion(new CellRangeAddress(startRow, endRow, colIndex, colIndex));sheet.addMergedRegion(new CellRangeAddress(startRow, endRow, colIndex-1, colIndex-1));}if(projectTreeVo.getIsLeaf() == 1){//遇到最子级时,+1创建新的一行rowIndex++;}return rowIndex;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词