|
|
@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.exceptions.DependencyException;
|
|
|
import cn.hutool.core.io.IORuntimeException;
|
|
|
+import cn.hutool.core.util.CharUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
|
|
import cn.hutool.poi.exceptions.POIException;
|
|
|
@@ -35,6 +36,20 @@ public class ExcelSaxUtil {
|
|
|
public static final int MAX_CELL_BIT = 3;
|
|
|
|
|
|
/**
|
|
|
+ * 创建 {@link ExcelSaxReader}
|
|
|
+ *
|
|
|
+ * @param isXlsx 是否为xlsx格式(07格式)
|
|
|
+ * @param rowHandler 行处理器
|
|
|
+ * @return {@link ExcelSaxReader}
|
|
|
+ * @since 5.4.4
|
|
|
+ */
|
|
|
+ public static ExcelSaxReader<?> createSaxReader(boolean isXlsx, RowHandler rowHandler) {
|
|
|
+ return isXlsx
|
|
|
+ ? new Excel07SaxReader(rowHandler)
|
|
|
+ : new Excel03SaxReader(rowHandler);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 根据数据类型获取数据
|
|
|
*
|
|
|
* @param cellDataType 数据类型枚举
|
|
|
@@ -175,12 +190,13 @@ public class ExcelSaxUtil {
|
|
|
|
|
|
/**
|
|
|
* 判断数字Record中是否为日期格式
|
|
|
- * @param cell 单元格记录
|
|
|
+ *
|
|
|
+ * @param cell 单元格记录
|
|
|
* @param formatListener {@link FormatTrackingHSSFListener}
|
|
|
* @return 是否为日期格式
|
|
|
* @since 5.4.8
|
|
|
*/
|
|
|
- public static boolean isDateFormat(CellValueRecordInterface cell, FormatTrackingHSSFListener formatListener){
|
|
|
+ public static boolean isDateFormat(CellValueRecordInterface cell, FormatTrackingHSSFListener formatListener) {
|
|
|
final int formatIndex = formatListener.getFormatIndex(cell);
|
|
|
final String formatString = formatListener.getFormatString(cell);
|
|
|
return isDateFormat(formatIndex, formatString);
|
|
|
@@ -189,15 +205,15 @@ public class ExcelSaxUtil {
|
|
|
/**
|
|
|
* 判断日期格式
|
|
|
*
|
|
|
- * @param formatIndex 格式索引,一般用于内建格式
|
|
|
+ * @param formatIndex 格式索引,一般用于内建格式
|
|
|
* @param formatString 格式字符串
|
|
|
* @return 是否为日期格式
|
|
|
* @since 5.5.3
|
|
|
*/
|
|
|
- public static boolean isDateFormat(int formatIndex, String formatString){
|
|
|
+ public static boolean isDateFormat(int formatIndex, String formatString) {
|
|
|
// https://blog.csdn.net/u014342130/article/details/50619503
|
|
|
// issue#1283@Github
|
|
|
- if(formatIndex == 28 || formatIndex == 31){
|
|
|
+ if (formatIndex == 28 || formatIndex == 31) {
|
|
|
// 28 -> m月d日
|
|
|
// 31 -> yyyy年m月d日
|
|
|
return true;
|
|
|
@@ -228,42 +244,21 @@ public class ExcelSaxUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 创建 {@link ExcelSaxReader}
|
|
|
- *
|
|
|
- * @param isXlsx 是否为xlsx格式(07格式)
|
|
|
- * @param rowHandler 行处理器
|
|
|
- * @return {@link ExcelSaxReader}
|
|
|
- * @since 5.4.4
|
|
|
- */
|
|
|
- public static ExcelSaxReader<?> createSaxReader(boolean isXlsx, RowHandler rowHandler) {
|
|
|
- return isXlsx
|
|
|
- ? new Excel07SaxReader(rowHandler)
|
|
|
- : new Excel03SaxReader(rowHandler);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 在Excel03 sax读取中获取日期或数字类型的结果值
|
|
|
- * @param cell 记录单元格
|
|
|
- * @param value 值
|
|
|
+ *
|
|
|
+ * @param cell 记录单元格
|
|
|
+ * @param value 值
|
|
|
* @param formatListener {@link FormatTrackingHSSFListener}
|
|
|
* @return 值,可能为Date或Double或Long
|
|
|
* @since 5.5.0
|
|
|
*/
|
|
|
- public static Object getNumberOrDateValue(CellValueRecordInterface cell, double value, FormatTrackingHSSFListener formatListener){
|
|
|
+ public static Object getNumberOrDateValue(CellValueRecordInterface cell, double value, FormatTrackingHSSFListener formatListener) {
|
|
|
Object result;
|
|
|
- if(ExcelSaxUtil.isDateFormat(cell, formatListener)){
|
|
|
+ if (isDateFormat(cell, formatListener)) {
|
|
|
// 可能为日期格式
|
|
|
- result = ExcelSaxUtil.getDateValue(value);
|
|
|
- } else {
|
|
|
- final long longPart = (long) value;
|
|
|
- // 对于无小数部分的数字类型,转为Long,否则保留原数字
|
|
|
- if (((double) longPart) == value) {
|
|
|
- result = longPart;
|
|
|
- } else {
|
|
|
- result = value;
|
|
|
- }
|
|
|
+ return getDateValue(value);
|
|
|
}
|
|
|
- return result;
|
|
|
+ return getNumberValue(value, formatListener.getFormatString(cell));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -278,9 +273,20 @@ public class ExcelSaxUtil {
|
|
|
if (StrUtil.isBlank(value)) {
|
|
|
return null;
|
|
|
}
|
|
|
- double numValue = Double.parseDouble(value);
|
|
|
+ return getNumberValue(Double.parseDouble(value), numFmtString);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数字类型值,除非格式中明确数字保留小数,否则无小数情况下按照long返回
|
|
|
+ *
|
|
|
+ * @param numValue 值
|
|
|
+ * @param numFmtString 格式
|
|
|
+ * @return 数字,可以是Double、Long
|
|
|
+ * @since 5.5.3
|
|
|
+ */
|
|
|
+ private static Number getNumberValue(double numValue, String numFmtString) {
|
|
|
// 普通数字
|
|
|
- if (null != numFmtString && numFmtString.indexOf(StrUtil.C_DOT) < 0) {
|
|
|
+ if (null != numFmtString && false == StrUtil.contains(numFmtString, CharUtil.DOT)) {
|
|
|
final long longPart = (long) numValue;
|
|
|
//noinspection RedundantIfStatement
|
|
|
if (longPart == numValue) {
|