This commit is contained in:
Looly 2023-05-26 21:28:31 +08:00
parent 2e01dd0183
commit 8ea3748b6f
4 changed files with 23 additions and 13 deletions

View File

@ -38,7 +38,7 @@
<druid.version>1.2.17</druid.version> <druid.version>1.2.17</druid.version>
<!-- 固定4.x --> <!-- 固定4.x -->
<hikariCP.version>4.0.3</hikariCP.version> <hikariCP.version>4.0.3</hikariCP.version>
<sqlite.version>3.41.2.1</sqlite.version> <sqlite.version>3.42.0.0</sqlite.version>
<!-- 此处固定2.5.x支持到JDK8 --> <!-- 此处固定2.5.x支持到JDK8 -->
<hsqldb.version>2.5.2</hsqldb.version> <hsqldb.version>2.5.2</hsqldb.version>
</properties> </properties>

View File

@ -64,8 +64,8 @@ public enum CellDataType {
*/ */
public static CellDataType of(final String name) { public static CellDataType of(final String name) {
if(null == name) { if(null == name) {
//默认数字 //默认
return NUMBER; return NULL;
} }
if(BOOL.name.equals(name)) { if(BOOL.name.equals(name)) {

View File

@ -12,15 +12,14 @@
package org.dromara.hutool.poi.excel.sax; 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.ss.usermodel.BuiltinFormats;
import org.apache.poi.xssf.model.SharedStrings; import org.apache.poi.xssf.model.SharedStrings;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; 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.Attributes;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
@ -259,9 +258,16 @@ public class SheetDataSaxHandler extends DefaultHandler {
padCell(preCoordinate, curCoordinate, false); padCell(preCoordinate, curCoordinate, false);
final String contentStr = StrUtil.trim(lastContent); final String contentStr = StrUtil.trim(lastContent);
Object value = ExcelSaxUtil.getDataValue(this.cellDataType, contentStr, this.sharedStrings, this.numFmtString); final Object value;
if (this.lastFormula.length() > 0) { if(this.lastFormula.length() > 0){
value = new FormulaCellValue(StrUtil.trim(lastFormula), value); 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); addCellValue(curCell++, value);
} }
@ -316,7 +322,10 @@ public class SheetDataSaxHandler extends DefaultHandler {
this.numFmtString = ObjUtil.defaultIfNull( this.numFmtString = ObjUtil.defaultIfNull(
xssfCellStyle.getDataFormatString(), xssfCellStyle.getDataFormatString(),
() -> BuiltinFormats.getBuiltinFormat(numFmtIndex)); () -> 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; cellDataType = CellDataType.DATE;
} }
} }

View File

@ -203,7 +203,8 @@ public class ExcelSaxReadTest {
Assertions.assertEquals("2020-10-09 00:00:00", rows.get(1)); Assertions.assertEquals("2020-10-09 00:00:00", rows.get(1));
// 非日期格式不做转换 // 非日期格式不做转换
Assertions.assertEquals("112233", rows.get(2)); 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)); Assertions.assertEquals("2012-12-21 00:00:00", rows.get(4));
} }