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

【csv写入】sheetname设置无效 #3755

Open
Xikl opened this issue Apr 8, 2024 · 0 comments
Open

【csv写入】sheetname设置无效 #3755

Xikl opened this issue Apr 8, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@Xikl
Copy link

Xikl commented Apr 8, 2024

version

3.3.4

触发场景描述

进行csv写入,设置了sheetname 但是最终生成出来的却不是自己的sheetname,看了下代码应该是以下这里有点问题,

// com.alibaba.excel.context.WriteContextImpl#createSheet
 private Sheet createSheet() {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Can not find sheet:{} ,now create it", writeSheetHolder.getSheetNo());
        }
       
        // 这里仅仅当sheetname为空的时候才会将sheetno赋值到sheetname中,其它的情况却没有赋值没有进行赋值
        if (StringUtils.isEmpty(writeSheetHolder.getSheetName())) {
            writeSheetHolder.setSheetName(writeSheetHolder.getSheetNo().toString());
        }
        Sheet currentSheet =
            WorkBookUtil.createSheet(writeWorkbookHolder.getWorkbook(), writeSheetHolder.getSheetName());
        writeSheetHolder.setCachedSheet(currentSheet);
        return currentSheet;
    }

触发Bug的代码

List<List<String>> data = Stream.of(
            Stream.of("d1", "d2", "d3").collect(Collectors.toList()),
            Stream.of("走", "d2", "d3").collect(Collectors.toList()),
            Stream.of("d1", "d2啥", "d3").collect(Collectors.toList()),
            Stream.of("d1", "d2", "d3的").collect(Collectors.toList())
        ).collect(Collectors.toList());

        List<List<String>> head = Stream.of(
                Stream.of("c1").collect(Collectors.toList()),
                Stream.of("c2").collect(Collectors.toList()),
                Stream.of("c3").collect(Collectors.toList())
        ).collect(Collectors.toList());



        EasyExcel.write("/Users/xx/Desktop/test_xxx_20240408.csv")
                .head(head)
                .excelType(ExcelTypeEnum.CSV)
                .charset(StandardCharsets.UTF_8)
                .sheet(0, "Sheet1")
                .doWrite(data);

解决方式

修改com.alibaba.excel.context.WriteContextImpl#createSheet的代码,赋值自己的sheetname

@Xikl Xikl added the bug Something isn't working label Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants