[alibaba/easyexcel]读取excel时出现类型转换异常

2024-05-11 225 views
2

easyexcel版本是2.1.6。在读取一个excel文件时出现ExcelDataConvertException异常,源代码如下:

SummaryReadListener summaryReadListener = new SummaryReadListener();
EasyExcel.read("C:\\1.xlsx", summaryReadListener).head(ResultSummary.class).sheet(0).doRead();

异常的详细信息如下:

com.alibaba.excel.exception.ExcelAnalysisException: com.alibaba.excel.exception.ExcelDataConvertException: Converter not found, convert STRING to String
    at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.parseXmlSource(XlsxSaxAnalyser.java:170)
    at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.execute(XlsxSaxAnalyser.java:189)
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:105)
    at com.alibaba.excel.ExcelReader.read(ExcelReader.java:180)
    at com.alibaba.excel.ExcelReader.read(ExcelReader.java:170)
    at com.alibaba.excel.read.builder.ExcelReaderSheetBuilder.doRead(ExcelReaderSheetBuilder.java:160)
    at test.Demo.main(Demo.java:17)
Caused by: com.alibaba.excel.exception.ExcelDataConvertException: Converter not found, convert STRING to String
    at com.alibaba.excel.util.ConverterUtils.convertToStringMap(ConverterUtils.java:55)
    at com.alibaba.excel.read.metadata.holder.AbstractReadHolder.buildHead(AbstractReadHolder.java:197)
    at com.alibaba.excel.read.metadata.holder.AbstractReadHolder.notifyEndOneRow(AbstractReadHolder.java:163)
    at com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler.endHandle(ProcessResultCellHandler.java:45)
    at com.alibaba.excel.analysis.v07.XlsxRowHandler.endElement(XlsxRowHandler.java:44)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.parseXmlSource(XlsxSaxAnalyser.java:165)
    ... 28 more

该异常是在生产环境出现的。在开发环境跟踪了代码,在初始化com.alibaba.excel.read.metadata.holder.AbstractReadHolder类时就把所有自带的Converter加载了,包括com.alibaba.excel.converters.string.StringStringConverter类,并没有出现该异常。

所以是什么原因会引起这个异常呢?

回答

9

能提供一下复现bug的所有源代码吗,包括excel文件

3

这是从项目中抽取出的代码。奇怪的是,我们自己也试过了,没有发现有异常。出了一个小demo到生产环境上测试那个出现异常的文件,也没有复现。 src.xlsx

public void read() {
    SummaryReadListener summaryReadListener = new SummaryReadListener();
    EasyExcel.read("C:\\Users\\huayu\\Desktop\\查询结果摘要解析失败\\wps.xlsx", summaryReadListener).head(ResultSummary.class)
        .sheet(0).doRead();
    List<ResultSummary> summaryList = summaryReadListener.getSummaryList();
    log.info("All Readed summary list: {}", JSON.toJSONString(summaryList, true));
}

// pojo类
@Data
public class ResultSummary {
    public static final int ROW_LIMIT = 10000;

    @ExcelProperty("审批表")
    private String cxh;

    @ExcelProperty("查询主体类别")
    private String cxztlb;

    @ExcelProperty("名称")
    private String mc;

    @ExcelProperty("证件类型")
    private String zjlx;

    @ExcelProperty("查询号码")
    private String cxhm;

    @ExcelProperty("协查单位")
    private String xcdw;

    @ExcelProperty("查询项")
    private String cxx;

    @ExcelProperty("查询状态")
    private String cxzt;

    @ExcelProperty("申请时间")
    private String sqsj;

    @ExcelProperty("反馈时间")
    private String fksj;

    @ExcelProperty("是否有结果")
    private String sfyjg;

    @ExcelProperty("交易流水数量")
    private String jylssl;

    @ExcelProperty("交易开始时间")
    private String jykssj;

    @ExcelProperty("交易结束时间")
    private String jyjssj;

    @ExcelProperty("查询对象标识")
    private String cxdxbs;

    @ExcelProperty("协查单位代码")
    private String xcdwdm;

    @ExcelProperty("查询项代码")
    private String cxxdm;

    @ExcelProperty("查询单号")
    private String cxdh;

    @ExcelIgnore
    private int rowNumber;

    public String getPrintedRowNumber() {
        if (rowNumber < ROW_LIMIT) {
            return String.valueOf(rowNumber);
        }
        return rowNumber / ROW_LIMIT + "、" + rowNumber % ROW_LIMIT;
    }
}

@Slf4j
public class SummaryReadListener extends AnalysisEventListener<ResultSummary> {
  private final List<ResultSummary> summaryList = new ArrayList<>();

  @Override
  public void invoke(ResultSummary data, AnalysisContext context) {
    Integer rowIndex = context.readRowHolder().getRowIndex();
    // 摘要行数,用于打印日志
    data.setRowNumber(rowIndex);
    //
    summaryList.add(data);
  }

  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    // do somthing
  }

  public List<ResultSummary> getSummaryList() {
    return summaryList;
  }
}
9

我在本地也没有复现该bug。下次再遇到的时候可以一起讨论一下 @Cqecc

8

升级到2.2.10试试