[alibaba/easyexcel]多集合数据源(上下层填充)时,会出现下层标题被覆盖问题

2024-04-26 608 views
3

假设我上层存在A集合数据源,A集合下方存在B集合数据源,都有各自的数据源标题头,做完数据动态填充后,下方B集合(包括标题头)应该是要随着A集合的数据情况向下移动

触发Bug的代码

public static void exportExcel(String templateId,WriteDirectionEnum writeDirectionEnum,  HttpServletResponse response, Object... dataList ) throws ApplicationException {

        ExcelWriter excelWriter = null;
        try{

            // 读取模板并做数据填充
            InputStream inputStream = FileUtils.readTemplate(templateId);
            excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(inputStream).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            fillConfig.setDirection(writeDirectionEnum);
            for (Object data: dataList) {

                if (Collection.class.isAssignableFrom(data.getClass()) || data.getClass().isArray()) {
                    excelWriter.fill(data, fillConfig, writeSheet);
                } else {
                    excelWriter.fill(data, writeSheet);
                }
            }

            // 导出 excel
            FileUtils.exportExcel(templateId,  response);
        } catch (Exception e) {
            throw new ApplicationException("NOMA_ERROR0005", templateId);
        } finally {
            if (null != excelWriter) {
                excelWriter.finish();
            }
        }
    }

使用版本:  <easyexcel.version>2.1.0-beta4</easyexcel.version>

image image

实际并没有出现这种效果,B集合的数据源向下移动了,但是其标题消失了? 验证了上层集合,下层对象,并没有出现类似的情况,这个是什么原因呢?

回答

1

建议贴出完整代码,或者提供一个可运行的demo

5

这个目前没法解决 后续版本会兼容。目前只能一个list

0

您好,我也遇到这样的问题。表头标题被覆盖。 微信截图_20191203154015 微信图片_20191203153916 这里是我相关demo地址: 地址

相关代码如下:

控制层:

    /*
    * 导出复杂对象
    * */
    @GetMapping("/exportComplexWithHorizen")
    public ResponseEntity<org.springframework.core.io.Resource> exportComplex(HttpServletResponse response,String fileName, String sheetName) throws IOException{

        try {
            return exportAndInportService.exportHorizonComplex(fileName,sheetName);
        } catch (IOException e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
        return null;
    }

业务层:

@Override
    public ResponseEntity<Resource> exportHorizonComplex(String fileName, String sheetName) throws IOException {
        //填充文件
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量
        String templateFileName = "complexTemplate.xlsx";
        fileName = fileName+".xlsx";
        ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        writeSheet.setSheetName(sheetName);

        //横向写
        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
        excelWriter.fill(data1(), fillConfig, writeSheet);
        excelWriter.fill(data2(),writeSheet);
        excelWriter.fill(data3(),writeSheet);
        excelWriter.fill(data4(),writeSheet);

        // 别忘记关闭流
        excelWriter.finish();

        //下载文件
        File dbfFile = new File(fileName);
        if (!dbfFile.exists() || dbfFile.isDirectory()) {
            dbfFile.createNewFile();
        }
        InputStreamResource resource = new InputStreamResource(new FileInputStream(dbfFile));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-disposition", "attachment;filename="+ fileName);

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(dbfFile.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(resource);
    }

    private QinComplexObject data1(){
        QinComplexObject complexObject = new QinComplexObject();
        complexObject.setModelCnName("模型一");
        complexObject.setModelEnName("model No.1");
        complexObject.setModelNumber("HB1235879530");
        complexObject.setModelStuff("小明");
        complexObject.setDevStuff("小芳");
        complexObject.setModelState("开发中");
        complexObject.setMinspace("100MB");
        complexObject.setMintime("2 days");
        complexObject.setRequiretime("4 days");
        complexObject.setOnlyexpression("说明一:这是一个说明");
        complexObject.setMainIndex("A分区 索引一");
        complexObject.setModelState("A");
        complexObject.setModelLevel("A");
        return complexObject;
    }
    private List<DepenceModel> data2(){
        List<DepenceModel> depenceModels = new ArrayList<>();
        DepenceModel depenceModel1 = new DepenceModel();
        DepenceModel depenceModel2 = new DepenceModel();
        depenceModel1.setModelEnName("Model 1");
        depenceModel1.setRelateDescription("这是一个相关描述一");
        depenceModel2.setModelEnName("Model 2");
        depenceModel2.setRelateDescription("这是一个相关描述二");
        depenceModels.add(depenceModel1);
        depenceModels.add(depenceModel2);
        return depenceModels;
    }
    private List<Detail> data3(){
        List<Detail> details = new ArrayList<>();
        Detail detail1 = new Detail();
        detail1.setId(1);
        detail1.setKeys(1003);
        detail1.setCnMark("标识一");
        detail1.setEnMark("mark one");
        detail1.setType("NUMBER");
        detail1.setLenght("1024");
        detail1.setUnit("cm");
        detail1.setDefaut("1024");
        detail1.setAttributesExpression("属性一");
        detail1.setDatasource("数据源表一");
        detail1.setDatasourceText("数据源表字段一");
        detail1.setAlgorithmExpression("算法描述一");
        detail1.setFunction("函数一");
        Detail detail2 = new Detail();
        detail2.setId(2);
        detail2.setKeys(1004);
        detail2.setCnMark("标识二");
        detail2.setEnMark("mark two");
        detail2.setType("NUMBER");
        detail2.setLenght("1024");
        detail2.setUnit("cm");
        detail2.setDefaut("1024");
        detail2.setAttributesExpression("属性二");
        detail2.setDatasource("数据源表二");
        detail2.setDatasourceText("数据源表字段二");
        detail2.setAlgorithmExpression("算法描述二");
        detail2.setFunction("函数二");
        details.add(detail1);
        details.add(detail2);
        return details;
    }
    private StoageDatabase data4(){
        StoageDatabase stoageDatabase = new StoageDatabase();
        stoageDatabase.setDatabase("MySql");
        stoageDatabase.setDatabaseName("名字一");
        stoageDatabase.setStorageCycle("两天");
        return stoageDatabase;
    }

模板和相关效果见刚刚两个图片。辛苦前辈

2

这个目前没法解决 后续版本会兼容。目前只能一个list

@zhuangjiaju 请问这个问题目前在开发计划中吗?大概什么时候能够支持多个list的填充?

1

请问解决了吗?我也是遇到这个问题 3以上的版本好像还是不行