[alibaba/easyexcel]使用自动换行导出的excel文件会提示错误

2024-04-30 785 views
9

//在填充完多结果数据后,要强制换行,不然后续的单结果数据会发生覆盖现象 FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();

    // 填充
    excelWriter.fill(productDevVo,writeSheet);
    excelWriter.fill(new FillWrapper("item",productDevItemVoList),fillConfig,writeSheet);
    excelWriter.fill(new FillWrapper("rep",productDevItemReplVoList),fillConfig,writeSheet);

回答

1

补充一下

8
            HttpServletResponse response = WebUtils.getResponse();
    response.setContentType("application/vnd.ms-excel; charset=utf-8");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("excel", "UTF-8").replaceAll("\\+", "%20");
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

    File file = ResourceUtils.getFile("classpath:template/importDev.xlsx");

    ExcelWriterBuilder writerBuilder = EasyExcel
            .write(response.getOutputStream())
            .withTemplate(file);

    WriteSheet writeSheet = writerBuilder.sheet().build();
    ExcelWriter excelWriter = writerBuilder.build();

    ProductDevVo productDevVo= getProductDevById(id);

    List<ProductDevItemVo> productDevItemVoList=getProductItemList(id);

    List<String> rePIds=productDevItemVoList.stream().map(ProductDevItemVo::getId).collect(Collectors.toList());
    List<ProductDevItemReplVo> productDevItemReplVoList=getProductRepl(rePIds);

    //在填充完多结果数据后,要强制换行,不然后续的单结果数据会发生覆盖现象
    FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();

    // 填充
    excelWriter.fill(productDevVo,writeSheet);
    excelWriter.fill(new FillWrapper("item",productDevItemVoList),fillConfig,writeSheet);
    excelWriter.fill(new FillWrapper("rep",productDevItemReplVoList),fillConfig,writeSheet);

    //调用finish方法
    excelWriter.finish();
1

完整代码

5

贴一份完整代码或者给一个demo