diff --git a/CHANGELOG.md b/CHANGELOG.md
index d63a06def..9feb0792c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@
### 新特性
* 【setting】 toBean改为泛型,增加class参数重载(pr#80@Gitee)
* 【core】 XmlUtil使用JDK默认的实现,避免第三方实现导致的问题(issue#I14ZS1@Gitee)
+* 【poi】 写入单元格数据类型支持jdk8日期格式(pr#628@Github)
### Bug修复
* 【core】 修复DateUtil.format使用DateTime时区失效问题(issue#I150I7@Gitee)
diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java
index f388a2431..e44d66abe 100644
--- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java
+++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java
@@ -18,13 +18,16 @@ import org.apache.poi.ss.util.SheetUtil;
import java.math.BigDecimal;
import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* Excel表格中单元格工具类
- *
+ *
* @author looly
* @since 4.0.7
*/
@@ -33,7 +36,7 @@ public class CellUtil {
/**
* 获取单元格值
- *
+ *
* @param cell {@link Cell}单元格
* @return 值,类型可能为:Date、Double、Boolean、String
* @since 4.6.3
@@ -44,8 +47,8 @@ public class CellUtil {
/**
* 获取单元格值
- *
- * @param cell {@link Cell}单元格
+ *
+ * @param cell {@link Cell}单元格
* @param isTrimCellValue 如果单元格类型为字符串,是否去掉两边空白符
* @return 值,类型可能为:Date、Double、Boolean、String
*/
@@ -58,8 +61,8 @@ public class CellUtil {
/**
* 获取单元格值
- *
- * @param cell {@link Cell}单元格
+ *
+ * @param cell {@link Cell}单元格
* @param cellEditor 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
* @return 值,类型可能为:Date、Double、Boolean、String
*/
@@ -72,9 +75,9 @@ public class CellUtil {
/**
* 获取单元格值
- *
- * @param cell {@link Cell}单元格
- * @param cellType 单元格值类型{@link CellType}枚举
+ *
+ * @param cell {@link Cell}单元格
+ * @param cellType 单元格值类型{@link CellType}枚举
* @param isTrimCellValue 如果单元格类型为字符串,是否去掉两边空白符
* @return 值,类型可能为:Date、Double、Boolean、String
*/
@@ -85,9 +88,9 @@ public class CellUtil {
/**
* 获取单元格值
* 如果单元格值为数字格式,则判断其格式中是否有小数部分,无则返回Long类型,否则返回Double类型
- *
- * @param cell {@link Cell}单元格
- * @param cellType 单元格值类型{@link CellType}枚举,如果为{@code null}默认使用cell的类型
+ *
+ * @param cell {@link Cell}单元格
+ * @param cellType 单元格值类型{@link CellType}枚举,如果为{@code null}默认使用cell的类型
* @param cellEditor 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
* @return 值,类型可能为:Date、Double、Boolean、String
*/
@@ -101,25 +104,25 @@ public class CellUtil {
Object value;
switch (cellType) {
- case NUMERIC:
- value = getNumericValue(cell);
- break;
- case BOOLEAN:
- value = cell.getBooleanCellValue();
- break;
- case FORMULA:
- // 遇到公式时查找公式结果类型
- value = getCellValue(cell, cell.getCachedFormulaResultTypeEnum(), cellEditor);
- break;
- case BLANK:
- value = StrUtil.EMPTY;
- break;
- case ERROR:
- final FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
- value = (null == error) ? StrUtil.EMPTY : error.getString();
- break;
- default:
- value = cell.getStringCellValue();
+ case NUMERIC:
+ value = getNumericValue(cell);
+ break;
+ case BOOLEAN:
+ value = cell.getBooleanCellValue();
+ break;
+ case FORMULA:
+ // 遇到公式时查找公式结果类型
+ value = getCellValue(cell, cell.getCachedFormulaResultTypeEnum(), cellEditor);
+ break;
+ case BLANK:
+ value = StrUtil.EMPTY;
+ break;
+ case ERROR:
+ final FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
+ value = (null == error) ? StrUtil.EMPTY : error.getString();
+ break;
+ default:
+ value = cell.getStringCellValue();
}
return null == cellEditor ? value : cellEditor.edit(cell, value);
@@ -129,17 +132,17 @@ public class CellUtil {
* 设置单元格值
* 根据传入的styleSet自动匹配样式
* 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
- *
- * @param cell 单元格
- * @param value 值
+ *
+ * @param cell 单元格
+ * @param value 值
* @param styleSet 单元格样式集,包括日期等样式
* @param isHeader 是否为标题单元格
*/
public static void setCellValue(Cell cell, Object value, StyleSet styleSet, boolean isHeader) {
- if(null == cell) {
+ if (null == cell) {
return;
}
-
+
if (null != styleSet) {
final CellStyle headCellStyle = styleSet.getHeadCellStyle();
final CellStyle cellStyle = styleSet.getCellStyle();
@@ -160,12 +163,21 @@ public class CellUtil {
cell.setCellStyle(styleSet.getCellStyleForDate());
}
cell.setCellValue((Date) value);
- } else if (value instanceof Instant) {
+ } else if (value instanceof TemporalAccessor) {
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
cell.setCellStyle(styleSet.getCellStyleForDate());
}
- cell.setCellValue(Date.from((Instant) value));
+ if (value instanceof Instant) {
+ cell.setCellValue(Date.from((Instant) value));
+ } else if (value instanceof LocalDateTime) {
+ cell.setCellValue((LocalDateTime) value);
+ } else if (value instanceof LocalDate) {
+ cell.setCellValue((LocalDate) value);
+ }
} else if (value instanceof Calendar) {
+ if (null != styleSet && null != styleSet.getCellStyleForDate()) {
+ cell.setCellStyle(styleSet.getCellStyleForDate());
+ }
cell.setCellValue((Calendar) value);
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean) value);
@@ -183,8 +195,8 @@ public class CellUtil {
/**
* 获取已有行或创建新行
- *
- * @param row Excel表的行
+ *
+ * @param row Excel表的行
* @param cellIndex 列号
* @return {@link Row}
* @since 4.0.2
@@ -199,9 +211,9 @@ public class CellUtil {
/**
* 判断指定的单元格是否是合并单元格
- *
- * @param sheet {@link Sheet}
- * @param row 行号
+ *
+ * @param sheet {@link Sheet}
+ * @param row 行号
* @param column 列号
* @return 是否是合并单元格
*/
@@ -219,13 +231,13 @@ public class CellUtil {
/**
* 合并单元格,可以根据设置的值来合并行和列
- *
- * @param sheet 表对象
- * @param firstRow 起始行,0开始
- * @param lastRow 结束行,0开始
+ *
+ * @param sheet 表对象
+ * @param firstRow 起始行,0开始
+ * @param lastRow 结束行,0开始
* @param firstColumn 起始列,0开始
- * @param lastColumn 结束列,0开始
- * @param cellStyle 单元格样式,只提取边框样式
+ * @param lastColumn 结束列,0开始
+ * @param cellStyle 单元格样式,只提取边框样式
* @return 合并后的单元格号
*/
public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn, CellStyle cellStyle) {
@@ -248,11 +260,10 @@ public class CellUtil {
/**
* 获取合并单元格的值
* 传入的x,y坐标(列行数)可以是合并单元格范围内的任意一个单元格
- *
*
* @param sheet {@link Sheet}
- * @param y 行号,从0开始,可以是合并单元格范围中的任意一行
- * @param x 列号,从0开始,可以是合并单元格范围中的任意一列
+ * @param y 行号,从0开始,可以是合并单元格范围中的任意一行
+ * @param x 列号,从0开始,可以是合并单元格范围中的任意一列
* @return 合并单元格的值
* @since 4.6.3
*/
@@ -280,9 +291,10 @@ public class CellUtil {
}
// -------------------------------------------------------------------------------------------------------------- Private method start
+
/**
* 获取数字类型的单元格值
- *
+ *
* @param cell 单元格
* @return 单元格值,可能为Long、Double、Date
*/
@@ -315,13 +327,13 @@ public class CellUtil {
/**
* 是否为日期格式
* 判断方式:
- *
+ *
*
* 1、指定序号 * 2、org.apache.poi.ss.usermodel.DateUtil.isADateFormat方法判定 *- * - * @param cell 单元格 + * + * @param cell 单元格 * @param formatIndex 格式序号 * @return 是否为日期格式 */