Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not close IO. #203

Open
hahaha-zsq opened this issue Jan 20, 2025 · 2 comments
Open

Can not close IO. #203

hahaha-zsq opened this issue Jan 20, 2025 · 2 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@hahaha-zsq
Copy link

      public void exportManySheetExcelWithEntity(HttpServletResponse response,
                                               List<EasyExcelManySheetModel> easyExcelManySheetModels,
                                               String fileName,
                                               String password
    ) {
        try {
            WebUtil.setDownLoadHeader(fileName, response);
            if (!ObjectUtils.isEmpty(easyExcelManySheetModels)) {
                @Cleanup ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).password(password).build();
                // 导出多sheet
                for (int i = 0; i < easyExcelManySheetModels.size(); i++) {
                    
                    ExcelWriterSheetBuilder excelWriterSheetBuilder = EasyExcel.writerSheet(i, easyExcelManySheetModels.get(i).getSheetName())
                            .head(easyExcelManySheetModels.get(i).getHead())
                            .automaticMergeHead(true)
                            .excludeColumnFieldNames(easyExcelManySheetModels.get(i).getExcludeColumnFiledNameList());
                    // 处理器
                    if (!ObjectUtils.isEmpty(easyExcelManySheetModels.get(i).getWriteHandlerList())) {
                        easyExcelManySheetModels.get(i).getWriteHandlerList().forEach(excelWriterSheetBuilder::registerWriteHandler);
                    } else {
                        excelWriterSheetBuilder.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy());
                    }
                    // 转换器
                    if (!ObjectUtils.isEmpty(easyExcelManySheetModels.get(i).getConverterList())) {
                        easyExcelManySheetModels.get(i).getConverterList().forEach(excelWriterSheetBuilder::registerConverter);
                    }
                    excelWriter.write(easyExcelManySheetModels.get(i).getDataList(), excelWriterSheetBuilder.build());
                }
                excelWriter.finish();
            }
        } catch (Exception e) {
            log.error("导出失败------------{}", e.getMessage());
            WebUtil.renderOutputStreamString(response, "导出失败");
        }
    }
    public static void setDownLoadHeader(String fileName, HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
       
        String fName = URLEncoder.encode(fileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
      
        response.setHeader("Content-disposition", "attachment;filename=" + fName);
      
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); 
    }
public static void renderOutputStreamString(HttpServletResponse response, String content) {
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            // 写入响应体
            response.getOutputStream().write(content.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class EasyExcelManySheetModel {

    /**
     * 表头所对应的实体类
     */
    private Class<?> head;
    /**
     * 泛型为表头实体类的集合数据
     */
    private List<?> dataList;
    /**
     *  easyExcel拦截器(自定义的,自带的)集合
     */
    private List<WriteHandler> writeHandlerList;

    /**
     * 自定义类型转换器列表
     */
    List<Converter<?>> converterList;
    /**
     * 排除列文件名列表
     */
    private List<String> excludeColumnFiledNameList;
    /**
     * sheet名称
     */
    private String sheetName;
    /**
     * 不使用实体类导出的表头数据映射map
     */
    Map<String, List<String>> headColumnMap;
    /**
     * 不使用实体类导出的数据集合
     */
    List<Map<String, Object>> dataMapList;
}
Image

不管是以前的easyExcxel还是现在的fastExcel:1.1.0版本都出现了上述问题

@psxjoy psxjoy self-assigned this Jan 20, 2025
@psxjoy psxjoy added the help wanted Extra attention is needed label Jan 20, 2025
@psxjoy
Copy link
Member

psxjoy commented Jan 20, 2025

I can’t confirm whether the issue is with your business logic. My suggestion is to simplify the logic first and try generating an Excel file with basic data. If it works, the issue is likely within your business logic. If not, there might be a conflict with the JAR files.

@wangmiscoding
Copy link
Contributor

@hahaha-zsq 有没有可能是导出处理时间太长,响应超时,前端关闭了响应,这个时候就会报这种异常

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants