[alibaba/easyexcel]文件后缀为 .xls 时报错 java.lang.IllegalArgumentException: col1 must be between 0 and 255, but was: -1

2024-05-11 15 views
0

触发场景描述

  • 文件后缀为 .xls 时,保存Excel文件到本地磁盘时报错
  • 文件后缀为 .xlsx 时,保存Excel文件到本地磁盘正常

环境信息

  • easyexcel 2.2.10
  • openjdk version "11.0.2" 2019-01-15
  • 操作系统
    • CentOS Linux release 8.2.2004
    • MacOS Big Sure 11.3.1 (20E241)

触发Bug的代码

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import lombok.Data;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ExportExcelBugTest {

    public static void main(String[] args) {
        List<ExcelBO> dataList = IntStream.range(0, 9)
                .mapToObj(i -> {
                    ExcelBO excel = new ExcelBO();
                    excel.setName("" + i);
                    return excel;
                })
                .collect(Collectors.toList());
        // 文件后缀为 .xls 时报错:Exception in thread "main" java.lang.IllegalArgumentException: col1 must be between 0 and 255, but was: -1
        // 文件后缀为 .xlsx 时正常
        save2Disk("/Users/aoe/Downloads/Excel有Bug.xls", "Bug", dataList, ExcelBO.class);
    }

    /**
     * 保存Excel文件到磁盘
     * @param fileName Excel 文件名称
     * @param sheetName sheet 名称
     * @param list 导出数据
     * @param clazz EasyExcel 对应的类
     */
    private static void save2Disk(String fileName, String sheetName, List list, Class clazz) {
        ExcelWriter excelWriter = EasyExcel.write(fileName,clazz).build();
        WriteSheet writeSheet = EasyExcelFactory.writerSheet(0, sheetName)
                .head(clazz)
                .registerWriteHandler(new FreezeAndFilter())
                .registerWriteHandler(getHorizontalCellStyleStrategy())
                .build();
        excelWriter.write(list, writeSheet);
        excelWriter.finish();
    }

    /**
     * 单元格策略
     * @return
     */
    private static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy() {
        // 标题策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());

        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteFont.setFontName("宋体");

        headWriteCellStyle.setWriteFont(headWriteFont);

        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();

        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 12);
        contentWriteFont.setFontName("宋体");

        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        // 设置 垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}

@Data
@HeadRowHeight(value = 40)
class ExcelBO{
    @ExcelProperty(value = "姓名", index = 0)
    @ColumnWidth(value = 18)
    private String name;
}

class FreezeAndFilter implements SheetWriteHandler {
    public int colSplit = 0, rowSplit = 1, leftmostColumn = 0, topRow = 1;
    public String autoFilterRange = "1:1";

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
        sheet.setAutoFilter(CellRangeAddress.valueOf(autoFilterRange));
    }
}

提示的异常或者没有达到的效果

  • 写入Excel失败,文件 size = 0
  • 报错信息
Exception in thread "main" java.lang.IllegalArgumentException: col1 must be between 0 and 255, but was: -1
    at org.apache.poi.hssf.usermodel.HSSFClientAnchor.checkRange(HSSFClientAnchor.java:273)
    at org.apache.poi.hssf.usermodel.HSSFClientAnchor.<init>(HSSFClientAnchor.java:70)
    at org.apache.poi.hssf.usermodel.HSSFSheet.setAutoFilter(HSSFSheet.java:2388)
    at org.apache.poi.hssf.usermodel.HSSFSheet.setAutoFilter(HSSFSheet.java:83)
    at FreezeAndFilter.afterSheetCreate(ExportExcelBugTest.java:118)
    at com.alibaba.excel.util.WriteHandlerUtils.afterSheetCreate(WriteHandlerUtils.java:103)
    at com.alibaba.excel.util.WriteHandlerUtils.afterSheetCreate(WriteHandlerUtils.java:93)
    at com.alibaba.excel.context.WriteContextImpl.initSheet(WriteContextImpl.java:188)
    at com.alibaba.excel.context.WriteContextImpl.currentSheet(WriteContextImpl.java:122)
    at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:53)
    at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161)
    at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146)
    at ExportExcelBugTest.save2Disk(ExportExcelBugTest.java:56)
    at ExportExcelBugTest.main(ExportExcelBugTest.java:39)

Process finished with exit code 1

回答

2

请问你能跑起来吗?我添加了各种依赖,还是跑不起来. 能不能把你的依赖给我看看啊?我按你的来配

0

请问你能跑起来吗?我添加了各种依赖,还是跑不起来. 能不能把你的依赖给我看看啊?我按你的来配

<!-- Excel处理 https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.10</version>
        </dependency>
1

谢谢你的分享. 但是我这儿仅仅引入easyexcel会报各种依赖冲突,导致前后引入了7,8个依赖,但最后还是报错. 我的jdk是1.8

7

public String autoFilterRange = "1:1"; 单元格是这么表示的 要"A1:B2"这种