add jd8 time support for poi

This commit is contained in:
Looly 2019-11-16 05:56:25 +08:00
parent 6b122734df
commit fdcf383bfc
2 changed files with 69 additions and 56 deletions

View File

@ -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

View File

@ -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 类型可能为DateDoubleBooleanString
* @since 4.6.3
@ -44,8 +47,8 @@ public class CellUtil {
/**
* 获取单元格值
*
* @param cell {@link Cell}单元格
*
* @param cell {@link Cell}单元格
* @param isTrimCellValue 如果单元格类型为字符串是否去掉两边空白符
* @return 类型可能为DateDoubleBooleanString
*/
@ -58,8 +61,8 @@ public class CellUtil {
/**
* 获取单元格值
*
* @param cell {@link Cell}单元格
*
* @param cell {@link Cell}单元格
* @param cellEditor 单元格值编辑器可以通过此编辑器对单元格值做自定义操作
* @return 类型可能为DateDoubleBooleanString
*/
@ -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 类型可能为DateDoubleBooleanString
*/
@ -85,9 +88,9 @@ public class CellUtil {
/**
* 获取单元格值<br>
* 如果单元格值为数字格式则判断其格式中是否有小数部分无则返回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 类型可能为DateDoubleBooleanString
*/
@ -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 {
* 设置单元格值<br>
* 根据传入的styleSet自动匹配样式<br>
* 当为头部样式时默认赋值头部样式但是头部中如果有数字日期等类型将按照数字日期样式设置
*
* @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 {
/**
* 获取合并单元格的值<br>
* 传入的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 单元格值可能为LongDoubleDate
*/
@ -315,13 +327,13 @@ public class CellUtil {
/**
* 是否为日期格式<br>
* 判断方式
*
*
* <pre>
* 1指定序号
* 2org.apache.poi.ss.usermodel.DateUtil.isADateFormat方法判定
* </pre>
*
* @param cell 单元格
*
* @param cell 单元格
* @param formatIndex 格式序号
* @return 是否为日期格式
*/