龙空技术网

EasyExcel 案例介绍一 复杂树形结构数据填充

JAVA果码 181

前言:

此时咱们对“excel表格树形结构”大致比较关怀,姐妹们都需要学习一些“excel表格树形结构”的相关文章。那么小编也在网上收集了一些有关“excel表格树形结构””的相关文章,希望姐妹们能喜欢,咱们快快来学习一下吧!

需求描述

需要导出excel表格,横坐标为层数,纵坐标也是层数,单元格内容为树形结构数据

示例

思路

计算出每个树形结构每个数据坐标进行递归填充,即可实现

伪代码实现

  //下载private void doDocDownLoad(HttpServletResponse response, Data data) {        response.setContentType(Constants.CONTENT_TYPE_EXCEL);        response.setCharacterEncoding(Charsets.UTF_8.name());        String fileName = data.getCompanyInfoData().getCompanyName() + "-股东穿透核查报告.xls";        // 防止中文乱码        String encodeFileName = URLEncoder.encode(fileName, Charsets.UTF_8).replaceAll("\\+", "%20");        response.setHeader(Constants.FIELD_CONTENT_DISPOSITION, "attachment;filename*=utf-8''" + encodeFileName);        String templatePath = "/static/template.xls";        try (InputStream inputStream = this.getClass().getResourceAsStream(templatePath)) {            ExcelWriter excelWriterAnalysis = EasyExcel.write(response.getOutputStream()).withTemplate(inputStream)                    .excelType(ExcelTypeEnum.XLS)                     // 下载树形列表            exeDownLoadMoreListData(excelWriterAnalysis, data);            //写入结束            excelWriterAnalysis.finish();        } catch (IOException e) {            log.error("文件下载失败", e);        }    }//执行下载private void exeDownLoadMoreListData(ExcelWriter excelWriterAnalysis,Data data) {         // 此为填充数据        WriteSheet writeSheet0= EasyExcel.writerSheet(0)                .registerWriteHandler(new SheetWriteHandler() {                    @Override                    public void afterSheetCreate(SheetWriteHandlerContext context) {                        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();                        Sheet sheet = workbook.getSheetAt(0);                  、   //代码略                        Map<String, Integer> map = new HashMap<>();                        map.put("rowNum", rowNum);                        //递归填充数据                        populateMoreListCellTree(layer, sheet, map);                        //填充头                        populateHead(Heads(), sheet, workbook);                    }                })                .build();        excelWriterAnalysis.fill("data", writeSheet0);    }         //递归填充内容      private void populateMoreListCellTree( Integer level, Sheet sheet, Map<String, Integer> map            , String location) {                //代码略                       //创建行                Row row = sheet.getRow(rowNum);                if (row == null) {                    row = sheet.createRow(rowNum);                }                //创建列                Cell cell = row.getCell(level);                if (cell == null) {                    cell = row.createCell(level);                }               //赋值单元格值                cell.setCellValue(location);                         populateMoreListCellTree(level,sheet, map,);    }             //填充头       private void populateHead(Head heads, Sheet sheet, Workbook workbook) {            //填充头            Row rowLevel = sheet.getRow(0);            if (rowLevel == null) {                rowLevel = sheet.createRow(0);            }            Cell cellLevel = rowLevel.getCell(1);            if (cellLevel == null) {                cellLevel = rowLevel.createCell(1);            }            cellLevel.setCellValue( "层");            //填充样式            CellStyle cellStyle = workbook.createCellStyle();            populateStyle(cellStyle, workbook, cellLevel);    }       //填充样式        private void populateStyle(CellStyle cellStyle, Workbook workbook, Cell cellHead) {        //填充格式        Font font = workbook.createFont();        //设置背景色        cellStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);        //设置居中        cellStyle.setAlignment(HorizontalAlignment.CENTER);        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);        //字体为白色        font.setColor(IndexedColors.WHITE.getIndex());        //设置字体大小        font.setFontHeightInPoints((short) 12);        //设置单元格长度        cellStyle.setFont(font);        cellHead.setCellStyle(cellStyle);    }    
结语

喜欢的朋友们,点点赞,留个关注,后面继续分享工作中遇到的实际应用问题案例

对于技术方面,朋友们有什么想学习的内容,可以分享在评论区,或许你的想学习的内容会在下一次分享中出现

标签: #excel表格树形结构