Browse Source

add jd8 time support for poi

Looly 6 years ago
parent
commit
fdcf383bfc
2 changed files with 69 additions and 56 deletions
  1. 1 0
      CHANGELOG.md
  2. 68 56
      hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java

+ 1 - 0
CHANGELOG.md

@@ -8,6 +8,7 @@
 ### 新特性
 ### 新特性
 * 【setting】    toBean改为泛型,增加class参数重载(pr#80@Gitee)
 * 【setting】    toBean改为泛型,增加class参数重载(pr#80@Gitee)
 * 【core】       XmlUtil使用JDK默认的实现,避免第三方实现导致的问题(issue#I14ZS1@Gitee)
 * 【core】       XmlUtil使用JDK默认的实现,避免第三方实现导致的问题(issue#I14ZS1@Gitee)
+* 【poi】        写入单元格数据类型支持jdk8日期格式(pr#628@Github)
 
 
 ### Bug修复
 ### Bug修复
 * 【core】       修复DateUtil.format使用DateTime时区失效问题(issue#I150I7@Gitee)
 * 【core】       修复DateUtil.format使用DateTime时区失效问题(issue#I150I7@Gitee)

+ 68 - 56
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.math.BigDecimal;
 import java.time.Instant;
 import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.temporal.TemporalAccessor;
 import java.util.Calendar;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Date;
 import java.util.List;
 import java.util.List;
 
 
 /**
 /**
  * Excel表格中单元格工具类
  * Excel表格中单元格工具类
- * 
+ *
  * @author looly
  * @author looly
  * @since 4.0.7
  * @since 4.0.7
  */
  */
@@ -33,7 +36,7 @@ public class CellUtil {
 
 
 	/**
 	/**
 	 * 获取单元格值
 	 * 获取单元格值
-	 * 
+	 *
 	 * @param cell {@link Cell}单元格
 	 * @param cell {@link Cell}单元格
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 * @since 4.6.3
 	 * @since 4.6.3
@@ -44,8 +47,8 @@ public class CellUtil {
 
 
 	/**
 	/**
 	 * 获取单元格值
 	 * 获取单元格值
-	 * 
-	 * @param cell {@link Cell}单元格
+	 *
+	 * @param cell            {@link Cell}单元格
 	 * @param isTrimCellValue 如果单元格类型为字符串,是否去掉两边空白符
 	 * @param isTrimCellValue 如果单元格类型为字符串,是否去掉两边空白符
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 */
 	 */
@@ -58,8 +61,8 @@ public class CellUtil {
 
 
 	/**
 	/**
 	 * 获取单元格值
 	 * 获取单元格值
-	 * 
-	 * @param cell {@link Cell}单元格
+	 *
+	 * @param cell       {@link Cell}单元格
 	 * @param cellEditor 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
 	 * @param cellEditor 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 * @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 如果单元格类型为字符串,是否去掉两边空白符
 	 * @param isTrimCellValue 如果单元格类型为字符串,是否去掉两边空白符
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 */
 	 */
@@ -85,9 +88,9 @@ public class CellUtil {
 	/**
 	/**
 	 * 获取单元格值<br>
 	 * 获取单元格值<br>
 	 * 如果单元格值为数字格式,则判断其格式中是否有小数部分,无则返回Long类型,否则返回Double类型
 	 * 如果单元格值为数字格式,则判断其格式中是否有小数部分,无则返回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 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
 	 * @param cellEditor 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 * @return 值,类型可能为:Date、Double、Boolean、String
 	 */
 	 */
@@ -101,25 +104,25 @@ public class CellUtil {
 
 
 		Object value;
 		Object value;
 		switch (cellType) {
 		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);
 		return null == cellEditor ? value : cellEditor.edit(cell, value);
@@ -129,17 +132,17 @@ public class CellUtil {
 	 * 设置单元格值<br>
 	 * 设置单元格值<br>
 	 * 根据传入的styleSet自动匹配样式<br>
 	 * 根据传入的styleSet自动匹配样式<br>
 	 * 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
 	 * 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
-	 * 
-	 * @param cell 单元格
-	 * @param value 值
+	 *
+	 * @param cell     单元格
+	 * @param value    
 	 * @param styleSet 单元格样式集,包括日期等样式
 	 * @param styleSet 单元格样式集,包括日期等样式
 	 * @param isHeader 是否为标题单元格
 	 * @param isHeader 是否为标题单元格
 	 */
 	 */
 	public static void setCellValue(Cell cell, Object value, StyleSet styleSet, boolean isHeader) {
 	public static void setCellValue(Cell cell, Object value, StyleSet styleSet, boolean isHeader) {
-		if(null == cell) {
+		if (null == cell) {
 			return;
 			return;
 		}
 		}
-		
+
 		if (null != styleSet) {
 		if (null != styleSet) {
 			final CellStyle headCellStyle = styleSet.getHeadCellStyle();
 			final CellStyle headCellStyle = styleSet.getHeadCellStyle();
 			final CellStyle cellStyle = styleSet.getCellStyle();
 			final CellStyle cellStyle = styleSet.getCellStyle();
@@ -160,12 +163,21 @@ public class CellUtil {
 				cell.setCellStyle(styleSet.getCellStyleForDate());
 				cell.setCellStyle(styleSet.getCellStyleForDate());
 			}
 			}
 			cell.setCellValue((Date) value);
 			cell.setCellValue((Date) value);
-		} else if (value instanceof Instant) {
+		} else if (value instanceof TemporalAccessor) {
 			if (null != styleSet && null != styleSet.getCellStyleForDate()) {
 			if (null != styleSet && null != styleSet.getCellStyleForDate()) {
 				cell.setCellStyle(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) {
 		} else if (value instanceof Calendar) {
+			if (null != styleSet && null != styleSet.getCellStyleForDate()) {
+				cell.setCellStyle(styleSet.getCellStyleForDate());
+			}
 			cell.setCellValue((Calendar) value);
 			cell.setCellValue((Calendar) value);
 		} else if (value instanceof Boolean) {
 		} else if (value instanceof Boolean) {
 			cell.setCellValue((Boolean) value);
 			cell.setCellValue((Boolean) value);
@@ -183,8 +195,8 @@ public class CellUtil {
 
 
 	/**
 	/**
 	 * 获取已有行或创建新行
 	 * 获取已有行或创建新行
-	 * 
-	 * @param row Excel表的行
+	 *
+	 * @param row       Excel表的行
 	 * @param cellIndex 列号
 	 * @param cellIndex 列号
 	 * @return {@link Row}
 	 * @return {@link Row}
 	 * @since 4.0.2
 	 * @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 列号
 	 * @param column 列号
 	 * @return 是否是合并单元格
 	 * @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 firstColumn 起始列,0开始
-	 * @param lastColumn 结束列,0开始
-	 * @param cellStyle 单元格样式,只提取边框样式
+	 * @param lastColumn  结束列,0开始
+	 * @param cellStyle   单元格样式,只提取边框样式
 	 * @return 合并后的单元格号
 	 * @return 合并后的单元格号
 	 */
 	 */
 	public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn, CellStyle cellStyle) {
 	public static int mergingCells(Sheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn, CellStyle cellStyle) {
@@ -248,11 +260,10 @@ public class CellUtil {
 	/**
 	/**
 	 * 获取合并单元格的值<br>
 	 * 获取合并单元格的值<br>
 	 * 传入的x,y坐标(列行数)可以是合并单元格范围内的任意一个单元格
 	 * 传入的x,y坐标(列行数)可以是合并单元格范围内的任意一个单元格
-	 * 
 	 *
 	 *
 	 * @param sheet {@link Sheet}
 	 * @param sheet {@link Sheet}
-	 * @param y 行号,从0开始,可以是合并单元格范围中的任意一行
-	 * @param x 列号,从0开始,可以是合并单元格范围中的任意一列
+	 * @param y     行号,从0开始,可以是合并单元格范围中的任意一行
+	 * @param x     列号,从0开始,可以是合并单元格范围中的任意一列
 	 * @return 合并单元格的值
 	 * @return 合并单元格的值
 	 * @since 4.6.3
 	 * @since 4.6.3
 	 */
 	 */
@@ -280,9 +291,10 @@ public class CellUtil {
 	}
 	}
 
 
 	// -------------------------------------------------------------------------------------------------------------- Private method start
 	// -------------------------------------------------------------------------------------------------------------- Private method start
+
 	/**
 	/**
 	 * 获取数字类型的单元格值
 	 * 获取数字类型的单元格值
-	 * 
+	 *
 	 * @param cell 单元格
 	 * @param cell 单元格
 	 * @return 单元格值,可能为Long、Double、Date
 	 * @return 单元格值,可能为Long、Double、Date
 	 */
 	 */
@@ -315,13 +327,13 @@ public class CellUtil {
 	/**
 	/**
 	 * 是否为日期格式<br>
 	 * 是否为日期格式<br>
 	 * 判断方式:
 	 * 判断方式:
-	 * 
+	 *
 	 * <pre>
 	 * <pre>
 	 * 1、指定序号
 	 * 1、指定序号
 	 * 2、org.apache.poi.ss.usermodel.DateUtil.isADateFormat方法判定
 	 * 2、org.apache.poi.ss.usermodel.DateUtil.isADateFormat方法判定
 	 * </pre>
 	 * </pre>
-	 * 
-	 * @param cell 单元格
+	 *
+	 * @param cell        单元格
 	 * @param formatIndex 格式序号
 	 * @param formatIndex 格式序号
 	 * @return 是否为日期格式
 	 * @return 是否为日期格式
 	 */
 	 */