[alibaba/easyexcel]导出excel时使用合并单元格策略,当需要合并的单元格数据较多时,给文件添加密码密码会报错误

2024-04-28 214 views
2

Snipaste_2023-03-30_16-13-08 image image

去掉密码可以正常导出excel文件

回答

5

writer的finish触发之后再执行你的业务逻辑

9

writer的finish触发之后再执行你的业务逻辑

finish方法之前都是在给文件写入数据,还没定位到问题在哪里

0

你不是把bytearrayoutputstream关了吗,这不是业务吗
这个要在finish之后关闭

4

你不是把bytearrayoutputstream关了吗,这不是业务吗 这个要在finish之后关闭

image 去掉还是一样的报错

6

你帖全代码让我能复现吧,你截图看不出来

9

你帖全代码让我能复现吧,你截图看不出来

@org.junit.jupiter.api.Test public void test1() {

    List<List<String>> headTitle = new ArrayList<>();
    headTitle.add(Lists.newArrayList("题目"));
    headTitle.add(Lists.newArrayList("选项"));
    headTitle.add(Lists.newArrayList("合计"));
    headTitle.add(Lists.newArrayList("比例(%)"));

    List<List<String>> list = new ArrayList<>();
    for (int i = 1; i <= 480; i++) {
        List<String> l = new ArrayList<>();
        l.add("测试");
        l.add(String.valueOf(i));
        l.add("0");
        l.add("0");
        list.add(l);
    }

    List<Integer> mergList = new ArrayList<>();
    for (int i = 1; i <= 40 ; i++) {
        mergList.add(12);
    }

    Map<String,List<List<String>>> map = new LinkedHashMap<>();

    map.put("问卷统计", list);
    // map.put("答题记录", contentData);
    // map.put("未答题记录", contentData1);
    Map<Integer,List<List<String>>> map1 = new LinkedHashMap<>();
    map1.put(0, headTitle);
    // map1.put(1, headTitles);
    // map1.put(2, relatedToMeHeadTitleList);

    exportReportMany(map,map1,"123456",mergList);

}

public void exportReportMany(Map<String, List<List<String>>> map,Map<Integer, List<List<String>>> headTitles,String password,List<Integer> merge) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ExcelWriter excelWriter = null;
    try {
        int i = 0;
        //不加密码不报错
        // excelWriter = EasyExcel.write(byteArrayOutputStream).build();
        excelWriter = EasyExcel.write(byteArrayOutputStream).password(password).build();

        for (String s : map.keySet()) {
            i++;
            WriteSheet writeSheet;
            if ("问卷统计".equals(s)) {
                System.out.println(1111222);
                writeSheet = EasyExcel.writerSheet(i - 1, s)
                        .head(headTitles.get(i - 1))
                        .registerWriteHandler(new QuestionSheet1Handler1(merge))
                        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                        .build();

            } else {
                writeSheet = EasyExcel.writerSheet(i - 1, s)
                        .head(headTitles.get(i - 1))
                        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                        .build();
            }
            excelWriter.write(map.get(s), writeSheet);
        }
        System.out.println(11111);
        excelWriter.finish();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (excelWriter != null) {
            excelWriter.finish();
        }
    }
}

}

class QuestionSheet1Handler1 extends AbstractMergeStrategy {

private List<Integer> merge;

public QuestionSheet1Handler1(List<Integer> merge) {
    this.merge = merge;
}

@Override
protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
    int start = 1;
    int end = 0;
    for (Integer i : merge) {
        start++;
        end += i;
        if (start == 2) {
            start = 1;
        }
        if (start < end) {
            CellRangeAddress cellRangeAddress = new CellRangeAddress(start, end, 0, 0);
            sheet.addMergedRegionUnsafe(cellRangeAddress);
        }
        start = end;
    }
}

}

3

你帖全代码让我能复现吧,你截图看不出来

这我写的测试的,easyexcel版本 3.1.1

3

你的QuestionSheet1Handler1类中 的merge方法 每一列都会调用一次,导致合并单元格重复覆盖的问题。你需要做一个判断让他只被执行一次一个流程,我简单改了一下你可以参照我的修改代码

 boolean success = false;

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
        if(success){
            return;
        }
        int start = 1;
        int end = 0;
        for (Integer i : merge) {
            start++;
            end += i;
            if (start == 2) {
                start = 1;
            }
            if (start < end) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(start, end, 0, 0);
                sheet.addMergedRegionUnsafe(cellRangeAddress);
            }
            start = end;
        }
        success = true;
    }
9

好的,感谢