mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add setCellValue method
This commit is contained in:
parent
83460af7ef
commit
0b6c01bac2
@ -153,20 +153,53 @@ public class CellUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value instanceof Date) {
|
||||||
|
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
|
||||||
|
cell.setCellStyle(styleSet.getCellStyleForDate());
|
||||||
|
}
|
||||||
|
} else if (value instanceof TemporalAccessor) {
|
||||||
|
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
|
||||||
|
cell.setCellStyle(styleSet.getCellStyleForDate());
|
||||||
|
}
|
||||||
|
} else if (value instanceof Calendar) {
|
||||||
|
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
|
||||||
|
cell.setCellStyle(styleSet.getCellStyleForDate());
|
||||||
|
}
|
||||||
|
} else if (value instanceof Number) {
|
||||||
|
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) {
|
||||||
|
cell.setCellStyle(styleSet.getCellStyleForNumber());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setCellValue(cell, value, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置单元格值<br>
|
||||||
|
* 根据传入的styleSet自动匹配样式<br>
|
||||||
|
* 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
|
||||||
|
*
|
||||||
|
* @param cell 单元格
|
||||||
|
* @param value 值
|
||||||
|
* @param style 自定义样式,null表示无样式
|
||||||
|
*/
|
||||||
|
public static void setCellValue(Cell cell, Object value, CellStyle style) {
|
||||||
|
if (null == cell) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null != style) {
|
||||||
|
cell.setCellStyle(style);
|
||||||
|
}
|
||||||
|
|
||||||
if (null == value) {
|
if (null == value) {
|
||||||
cell.setCellValue(StrUtil.EMPTY);
|
cell.setCellValue(StrUtil.EMPTY);
|
||||||
} else if (value instanceof FormulaCellValue) {
|
} else if (value instanceof FormulaCellValue) {
|
||||||
// 公式
|
// 公式
|
||||||
cell.setCellFormula(((FormulaCellValue) value).getValue());
|
cell.setCellFormula(((FormulaCellValue) value).getValue());
|
||||||
} else if (value instanceof Date) {
|
} else if (value instanceof Date) {
|
||||||
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
|
|
||||||
cell.setCellStyle(styleSet.getCellStyleForDate());
|
|
||||||
}
|
|
||||||
cell.setCellValue((Date) value);
|
cell.setCellValue((Date) value);
|
||||||
} else if (value instanceof TemporalAccessor) {
|
} else if (value instanceof TemporalAccessor) {
|
||||||
if (null != styleSet && null != styleSet.getCellStyleForDate()) {
|
|
||||||
cell.setCellStyle(styleSet.getCellStyleForDate());
|
|
||||||
}
|
|
||||||
if (value instanceof Instant) {
|
if (value instanceof Instant) {
|
||||||
cell.setCellValue(Date.from((Instant) value));
|
cell.setCellValue(Date.from((Instant) value));
|
||||||
} else if (value instanceof LocalDateTime) {
|
} else if (value instanceof LocalDateTime) {
|
||||||
@ -175,18 +208,12 @@ public class CellUtil {
|
|||||||
cell.setCellValue((LocalDate) value);
|
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);
|
||||||
} else if (value instanceof RichTextString) {
|
} else if (value instanceof RichTextString) {
|
||||||
cell.setCellValue((RichTextString) value);
|
cell.setCellValue((RichTextString) value);
|
||||||
} else if (value instanceof Number) {
|
} else if (value instanceof Number) {
|
||||||
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) {
|
|
||||||
cell.setCellStyle(styleSet.getCellStyleForNumber());
|
|
||||||
}
|
|
||||||
cell.setCellValue(((Number) value).doubleValue());
|
cell.setCellValue(((Number) value).doubleValue());
|
||||||
} else {
|
} else {
|
||||||
cell.setCellValue(value.toString());
|
cell.setCellValue(value.toString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user