[alibaba/easyexcel]为什么有的excel可以,有的解析不了?

2024-04-28 466 views
7
异常代码
   public class EasyExcelTestObj {
    public static void main(String[] args) throws FileNotFoundException {
        testStudentRead("C:\\Users\\Administrator\\Desktop\\图纸解析\\H7D09353.xls");
    }

    public static void testStudentRead(String targetFilePath) throws FileNotFoundException {
        List<Map<String, String>> dataList = new ArrayList<>();
        // 读取excel
        EasyExcelStudentListener listener = new EasyExcelStudentListener(dataList);
        EasyExcel.read(targetFilePath, listener).sheet(0).headRowNumber(4).doRead();
        // 输出读到的内容
        for (Map dto : dataList) {
            System.out.println(JSON.toJSONString(dto));
        }
    }
异常提示

Exception in thread "main" java.lang.NullPointerException at com.alibaba.excel.analysis.v03.handlers.DummyRecordHandler.processRecord(DummyRecordHandler.java:29) at com.alibaba.excel.analysis.v03.XlsSaxAnalyser.processRecord(XlsSaxAnalyser.java:157) at org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener.processRecord(MissingRecordAwareHSSFListener.java:156) at org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener.processRecord(FormatTrackingHSSFListener.java:92) at org.apache.poi.hssf.eventusermodel.HSSFRequest.processRecord(HSSFRequest.java:106)

问题描述

这是我的excel文件 123.xls

回答

6

EasyExcelStudentListener是什么,代码贴全

3

@AllArgsConstructor @Slf4j public class EasyExcelStudentListener extends AnalysisEventListener<Map<Integer, String>> { private List<Map<String, String>> dataList; private List<Map<Integer, String>> headList;

public EasyExcelStudentListener(List dataList){
    this.dataList = dataList;
    headList = new ArrayList<>();
}

@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    headList.add(headMap);

    // 需要处理表头的可以在这里处理
    for (Integer key : headMap.keySet()) {
        System.out.print(key + ":" + headMap.get(key) + "\t");
    }
}

@Override
public void invoke(Map<Integer, String> data, AnalysisContext context) {
    if (data != null && data.get(2) != null
            && data.get(2).toString().equalsIgnoreCase("LHVR-45KFD/G2FZBP/P")) {  //  定位到行

        Map<String, String> result = new HashMap<>();
        Map<Integer, String> firstHeadMap = headList.get(0);
        Map<Integer, String> seconHeadMap = headList.get(1);
        String lastValue1 = "";

        for (Integer key : firstHeadMap.keySet()) {
            String value1 = firstHeadMap.get(key);
            String value2 = seconHeadMap.get(key);

            if(value1 != null){
                lastValue1 = value1;
            }
            if(value1 != null && value1.contains("产品型号")){
                result.put("xinghao", data.get(key));
            }
        }
        dataList.add(result);
    }
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}

}

8

@gongxuanzhang 谢谢! EasyExcelStudentListener就是一个普通的listener。如果新建一个excel,将上面要解析的excel内容拷进去,新建的excel是可以正常读取的。

4

你改成xlsx吧

5

图片

改成xlsx不行

4

是存成xlsx 不是把文件后缀改成xlsx就完了

5

存成xlsx可以了,能告诉我是什么原因吗?因为源头的excel是动态生成出来的。

2

是因为excel里面有宏吗?我用poi也做了测试,得到的都是跟宏相关的信息。 图片

6

可能是poi对xls的解析有冲突,解决了就好