[alibaba/easyexcel]映射报错java.lang.ClassCastException: java.util.ArrayList cannot be cast to ?

2024-06-20 984 views
2

V1.1.2-beta5


        // 解析每行结果在listener中处理
        ExcelListener listener = new ExcelListener();
        ExcelReader excelReader = new ExcelReader(inputStream,null, listener,true);
        excelReader.read(new Sheet(2, 1, CategoryModel.class));

public class ExcelListener extends AnalysisEventListener{

@Override
public void invoke(CategoryModel object, AnalysisContext context) {
    System.out.println("当前行: " + context.getCurrentRowNum());
    System.out.println(object);
}
    @Override
public void doAfterAllAnalysed(AnalysisContext context) {
    // TODO Auto-generated method stub

}

}


public class CategoryModel extends BaseRowModel { @ExcelProperty(index=2) private String catagory_v1; @ExcelProperty(index=3) private String catagory_v2; @ExcelProperty(index=4) private String catagory_v3; }


java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.CategoryModel at com.enation.app.shop.excel.ExcelListener.invoke(ExcelListener.java:1) at com.alibaba.excel.analysis.BaseSaxAnalyser.notifyListeners(BaseSaxAnalyser.java:72) at com.alibaba.excel.analysis.v07.XlsxRowHandler.endRow(XlsxRowHandler.java:132) at com.alibaba.excel.analysis.v07.XlsxRowHandler.endElement(XlsxRowHandler.java:110) 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:841) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770) 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:97) at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.execute(XlsxSaxAnalyser.java:81) at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:74) at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:67) at com.alibaba.excel.ExcelReader.read(ExcelReader.java:112) at com.enation.app.shop.excel.TestImport.main(TestImport.java:23)

回答

8

写法有问题吧

  /**
     * 07版本excel读数据量大于1千行,内部采用回调方法.
     *
     * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流
     */
    @Test
    public void saxReadJavaModelV2007() throws IOException {
        InputStream inputStream = FileUtil.getResourcesFileInputStream("测试.xlsx");
        ExcelListener excelListener = new ExcelListener();
        EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener);
        inputStream.close();
    }
6

我的也是这个错,不过我是EasyExcelFactory.read的。debug看返回的list里是个list,不是Model