[alibaba/easyexcel]解析表头时NullPointerException

2024-05-17 515 views
4

**解析Excel表头时出现的异常

**

触发Bug的代码

private static class HeadDataListener extends AnalysisEventListener<Map<Integer, String>> {
        Map<Integer, String> headMapResult=new HashMap<>();

        @Override
        public void invoke(Map<Integer, String> data, AnalysisContext context) {

        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {

        }

        /**
         * 这里会一行行的返回头
         *
         * @param headMap
         * @param context
         */
        @Override
        public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
            headMapResult=headMap;
        }

        public Map<Integer, String> getHeadMapResult() {
            return headMapResult;
        }

        public void setHeadMapResult(Map<Integer, String> headMapResult) {
            this.headMapResult = headMapResult;
        }
    }  

public static Map<Integer, String> getExcelHeader(InputStream inputStream){
        HeadDataListener headDataListener = new HeadDataListener();
        EasyExcelFactory.read(inputStream, headDataListener).build().readAll();
        return headDataListener.getHeadMapResult();
    }
```java
java.lang.NullPointerException: null
    at org.apache.poi.xssf.usermodel.XSSFTableStyle.<init>(XSSFTableStyle.java:64) ~[poi-ooxml-3.17.jar:3.17]
    at org.apache.poi.xssf.model.StylesTable.readFrom(StylesTable.java:247) ~[poi-ooxml-3.17.jar:3.17]
    at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:141) ~[poi-ooxml-3.17.jar:3.17]
    at org.apache.poi.xssf.eventusermodel.XSSFReader.getStylesTable(XSSFReader.java:126) ~[poi-ooxml-3.17.jar:3.17]
    at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.<init>(XlsxSaxAnalyser.java:81) ~[easyexcel-2.2.6.jar:na]
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.choiceExcelExecutor(ExcelAnalyserImpl.java:99) ~[easyexcel-2.2.6.jar:na]
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.<init>(ExcelAnalyserImpl.java:51) ~[easyexcel-2.2.6.jar:na]
    at com.alibaba.excel.ExcelReader.<init>(ExcelReader.java:145) ~[easyexcel-2.2.6.jar:na]
    at com.alibaba.excel.read.builder.ExcelReaderBuilder.build(ExcelReaderBuilder.java:193) ~[easyexcel-2.2.6.jar:na]

原文件如下 easyexcel_error.xlsx

回答

4

贴你的导出类看看。

1

贴你的导出类看看。

public static Result existExcelHeader(InputStream inputStream, String fileName, StudentTypeEnum studentTypeEnum) {
        if (!validateExcel(fileName)) {
            return Result.fail("文件不是excel格式");
        }
        final int maxHeaderCount=20;
        List<String> headerListByReadExcel= new ArrayList<>(16);
        List<String> mustHeaderList = new ArrayList<>();
        try {
            switch (studentTypeEnum) {
                case YES_MAINLAND_HAS_ID_CARD:
                    Map<Integer, String> excelHeaderByHasIdCard = EasyExcelUtil.getExcelHeader(inputStream);
                    for (Map.Entry<Integer, String> entry : excelHeaderByHasIdCard.entrySet()) {
                        Integer key = entry.getKey();
                        if (key > maxHeaderCount) {
                            continue;
                        }
                        String value = entry.getValue();
                        headerListByReadExcel.add(value);
                    }
                    mustHeaderList = new ArrayList<>(Arrays.asList(MAINLAND_HAS_ID_CARD_EXCEL_HEADER));
                    excelHeaderByHasIdCard = null;
                    break;
                case YES_MAINLAND_NO_ID_CARD:
                    Map<Integer, String> excelHeaderByNoIdCard = EasyExcelUtil.getExcelHeader(inputStream);
                    for (Map.Entry<Integer, String> entry : excelHeaderByNoIdCard.entrySet()) {
                        Integer key = entry.getKey();
                        if (key > maxHeaderCount) {
                            continue;
                        }
                        String value = entry.getValue();
                        headerListByReadExcel.add(value);
                    }
                    mustHeaderList = new ArrayList<>(Arrays.asList(MAINLAND_NO_ID_CARD_EXCEL_HEADER));
                    excelHeaderByNoIdCard = null;
                    break;
                case NO_MAINLAND:
                    Map<Integer, String> excelHeaderByNonMainland = EasyExcelUtil.getExcelHeader(inputStream);
                    for (Map.Entry<Integer, String> entry : excelHeaderByNonMainland.entrySet()) {
                        Integer key = entry.getKey();
                        if (key > maxHeaderCount) {
                            continue;
                        }
                        String value = entry.getValue();
                        headerListByReadExcel.add(value);
                    }
                    mustHeaderList = new ArrayList<>(Arrays.asList(NON_MAINLAND_EXCEL_HEADER));
                    excelHeaderByNonMainland = null;
                    break;
                default:
                    break;
            }
        }catch (Exception e){
            return Result.fail("表头检索失败,请下载相应Excel模板进行填充数据或复制Excel模板的表头至当前文件中");
        }
        if(CollectionUtils.isEmpty(headerListByReadExcel)){
            return Result.fail("表头" + mustHeaderList.toString()+ "缺失");
        }
        mustHeaderList.removeIf(headerListByReadExcel::contains);
        if(!CollectionUtils.isEmpty(mustHeaderList)){
            return Result.fail("表头" + mustHeaderList.toString()+ "缺失");
        }
        return Result.succeed();
    }
6

重新保存这个问就行。请使用 正版office 或者 wps 创建文件。