diff --git a/hutool-db/pom.xml b/hutool-db/pom.xml index e3795b358..11be85567 100755 --- a/hutool-db/pom.xml +++ b/hutool-db/pom.xml @@ -38,7 +38,7 @@ 1.2.17 4.0.3 - 3.41.2.1 + 3.42.0.0 2.5.2 diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/CellDataType.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/CellDataType.java index 9413283c9..7acdcfa02 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/CellDataType.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/CellDataType.java @@ -64,8 +64,8 @@ public enum CellDataType { */ public static CellDataType of(final String name) { if(null == name) { - //默认数字 - return NUMBER; + //默认空 + return NULL; } if(BOOL.name.equals(name)) { diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/SheetDataSaxHandler.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/SheetDataSaxHandler.java index a4dd8ea71..07de1ddfb 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/SheetDataSaxHandler.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/SheetDataSaxHandler.java @@ -12,15 +12,14 @@ package org.dromara.hutool.poi.excel.sax; -import org.dromara.hutool.core.lang.Console; -import org.dromara.hutool.core.text.StrUtil; -import org.dromara.hutool.core.util.ObjUtil; -import org.dromara.hutool.poi.excel.cell.values.FormulaCellValue; -import org.dromara.hutool.poi.excel.sax.handler.RowHandler; import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.xssf.model.SharedStrings; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.core.util.ObjUtil; +import org.dromara.hutool.poi.excel.cell.values.FormulaCellValue; +import org.dromara.hutool.poi.excel.sax.handler.RowHandler; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; @@ -259,9 +258,16 @@ public class SheetDataSaxHandler extends DefaultHandler { padCell(preCoordinate, curCoordinate, false); final String contentStr = StrUtil.trim(lastContent); - Object value = ExcelSaxUtil.getDataValue(this.cellDataType, contentStr, this.sharedStrings, this.numFmtString); - if (this.lastFormula.length() > 0) { - value = new FormulaCellValue(StrUtil.trim(lastFormula), value); + final Object value; + if(this.lastFormula.length() > 0){ + if(CellDataType.NULL == this.cellDataType){ + // 对于公式,默认值类型为数字 + this.cellDataType = CellDataType.NUMBER; + } + value = new FormulaCellValue(StrUtil.trim(lastFormula), + ExcelSaxUtil.getDataValue(this.cellDataType, contentStr, this.sharedStrings, this.numFmtString)); + }else{ + value = ExcelSaxUtil.getDataValue(this.cellDataType, contentStr, this.sharedStrings, this.numFmtString); } addCellValue(curCell++, value); } @@ -316,7 +322,10 @@ public class SheetDataSaxHandler extends DefaultHandler { this.numFmtString = ObjUtil.defaultIfNull( xssfCellStyle.getDataFormatString(), () -> BuiltinFormats.getBuiltinFormat(numFmtIndex)); - if (CellDataType.NUMBER == this.cellDataType && ExcelSaxUtil.isDateFormat(numFmtIndex, numFmtString)) { + + // 日期格式的单元格可能没有t元素 + if ((CellDataType.NUMBER == this.cellDataType || CellDataType.NULL == this.cellDataType) + && ExcelSaxUtil.isDateFormat(numFmtIndex, numFmtString)) { cellDataType = CellDataType.DATE; } } diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelSaxReadTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelSaxReadTest.java index 06899b1fa..a8e817173 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelSaxReadTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelSaxReadTest.java @@ -203,7 +203,8 @@ public class ExcelSaxReadTest { Assertions.assertEquals("2020-10-09 00:00:00", rows.get(1)); // 非日期格式不做转换 Assertions.assertEquals("112233", rows.get(2)); - Assertions.assertEquals("1000.0", rows.get(3)); + // 读取实际值,而非带有格式处理过的值 + Assertions.assertEquals("1000", rows.get(3)); Assertions.assertEquals("2012-12-21 00:00:00", rows.get(4)); }