diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index 2b67b439..a5072a1d 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -131,6 +131,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor { thisColumn = handler.getColumn(); cellData = handler.getCellData(); if (cellData != null) { + cellData.checkEmpty(); records.put(thisColumn, cellData); } break; diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index 671693b4..0ee8ab3a 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -120,6 +120,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder } else if (currentCellData.getType() == CellDataTypeEnum.DIRECT_STRING) { currentCellData.setType(CellDataTypeEnum.STRING); } + currentCellData.checkEmpty(); curRowContent.put(curCol, currentCellData); } // This is a special form of string @@ -130,6 +131,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder stringValue = stringValue.trim(); } currentCellData.setStringValue(stringValue); + currentCellData.checkEmpty(); curRowContent.put(curCol, currentCellData); } } diff --git a/src/main/java/com/alibaba/excel/metadata/CellData.java b/src/main/java/com/alibaba/excel/metadata/CellData.java index db73ce8f..adfb091a 100644 --- a/src/main/java/com/alibaba/excel/metadata/CellData.java +++ b/src/main/java/com/alibaba/excel/metadata/CellData.java @@ -168,6 +168,31 @@ public class CellData { this.dataFormatString = dataFormatString; } + /** + * Ensure that the object does not appear null + */ + public void checkEmpty() { + switch (type) { + case STRING: + case ERROR: + if (stringValue == null) { + type = CellDataTypeEnum.EMPTY; + } + return; + case NUMBER: + if (doubleValue == null) { + type = CellDataTypeEnum.EMPTY; + } + return; + case BOOLEAN: + if (booleanValue == null) { + type = CellDataTypeEnum.EMPTY; + } + return; + default: + } + } + @Override public String toString() { switch (type) {