alibaba/easyexcel

3.3.4 版本,使用excelWriter.write(data, sheet1)的时候,data数据如果是map的话,写不进去数据

Open

#3,999 opened on Sep 19, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (7,599 forks)batch import
help wanted

Repository metrics

Stars
 (33,728 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

建议先去看文档

快速开始常见问题

异常代码

 // 获取导出模板需要的数据
        List<JSONObject> data = this.getExportExcelTemplateData(itemImportDto, detailList, excelItemByConfigRpcService);
        // 包含的行
        List<String> includeColumnSet = detailList.stream().map(ExcelItem::getColumnCode).collect(toList());;
        try (OutputStream outputStream = response.getOutputStream()) {
            response.setContentType("application/force-download");
            String name = CharSequenceUtil.emptyToDefault(itemImportDto.getName(), "导入模板") + ".xls";
            response.addHeader("Content-Disposition", "attachment;fileName=" + new String(name.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
            WriteSheet sheet1 = EasyExcel.writerSheet(0, "data")
                    .registerWriteHandler(new ExcelCellStyleStrategy(detailList))
                    .registerWriteHandler(ExcelCellStyleStrategy.TEMPLATE_WRITE_HANDLER)
                    .registerWriteHandler(new ExcelDictWriteHandler(detailList, this, itemImportDto.getBusAccount()))
                    .includeColumnFieldNames(includeColumnSet)
                    .head(ExcelUtil.getHeaderList(detailList))
                    .build();
            excelWriter.write(data, sheet1);


ExcelWriteAddExecutor:
 private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex) {
        if (oneRowData == null) {
            return;
        }
        RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext,
                rowIndex, relativeRowIndex, Boolean.FALSE);
        WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext);

        Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex);
        rowWriteHandlerContext.setRow(row);

        WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext);

        if (oneRowData instanceof Collection<?>) {
            addBasicTypeToExcel(new CollectionRowData((Collection<?>) oneRowData), row, rowIndex, relativeRowIndex);
        }
//        else if (oneRowData instanceof JSONObject || oneRowData instanceof com.alibaba.fastjson.JSONObject) {
//            addBasicTypeToExcelNew(new StringMapRowData((Map<String, ?>) oneRowData), row, rowIndex, relativeRowIndex);
//        }
        else if (oneRowData instanceof Map) {
            // 这个jsonobject 强转成new MapRowData((Map<Integer, ?>) oneRowData),使用get(index) 怎么可能可以取到值?
            addBasicTypeToExcel(new MapRowData((Map<Integer, ?>) oneRowData), row, rowIndex, relativeRowIndex);
        } else {
            addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex);
        }

        WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
    }

异常提示

data为jsonObject或者map的时候,传进去进入执行器,走的map逻辑,但是代码强转呈rowData,只是这个要求key是interger啊,估计是要求列对应这个map里面,使用列index作为key,但是这样感觉非常不合理,我使用map瑶瑶特意改成map<integer,Object> 类型 而且,ExcelWriteAddExecutor初始化还不放出来,覆盖写的话,也是就、打包的话改了还没用,自己还得在启动类重写这个类,太恶心了 大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。

问题描述

Contributor guide