mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
fix code
This commit is contained in:
parent
2e01dd0183
commit
8ea3748b6f
@ -38,7 +38,7 @@
|
||||
<druid.version>1.2.17</druid.version>
|
||||
<!-- 固定4.x -->
|
||||
<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 -->
|
||||
<hsqldb.version>2.5.2</hsqldb.version>
|
||||
</properties>
|
||||
|
@ -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)) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user