Browse Source

add BigDecimal support

Looly 6 years ago
parent
commit
796fac2458
2 changed files with 11 additions and 7 deletions
  1. 1 0
      CHANGELOG.md
  2. 10 7
      hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java

+ 1 - 0
CHANGELOG.md

@@ -17,6 +17,7 @@
 * 【core】         增强日期工具类(pr#455@Github)
 * 【setting】      构造Setting增加默认字符编码
 * 【extra】        ServletUtil增加getHeaderMap方法
+* 【poi】           CellUtil改进数字支持,解决空指针问题(pr#489@Github)
 
 ### Bug修复
 * 【cache】       修复missCount规则(issue#465@Github)

+ 10 - 7
hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java

@@ -1,5 +1,6 @@
 package cn.hutool.poi.excel.cell;
 
+import java.math.BigDecimal;
 import java.util.Calendar;
 import java.util.Date;
 
@@ -120,12 +121,14 @@ public class CellUtil {
 	 * @param isHeader 是否为标题单元格
 	 */
 	public static void setCellValue(Cell cell, Object value, StyleSet styleSet, boolean isHeader) {
-		final CellStyle headCellStyle = styleSet.getHeadCellStyle();
-		final CellStyle cellStyle = styleSet.getCellStyle();
-		if (isHeader && null != headCellStyle) {
-			cell.setCellStyle(headCellStyle);
-		} else if (null != cellStyle) {
-			cell.setCellStyle(cellStyle);
+		if(null != styleSet) {
+			final CellStyle headCellStyle = styleSet.getHeadCellStyle();
+			final CellStyle cellStyle = styleSet.getCellStyle();
+			if (isHeader && null != headCellStyle) {
+				cell.setCellStyle(headCellStyle);
+			} else if (null != cellStyle) {
+				cell.setCellStyle(cellStyle);
+			}
 		}
 
 		if (null == value) {
@@ -145,7 +148,7 @@ public class CellUtil {
 		} else if (value instanceof RichTextString) {
 			cell.setCellValue((RichTextString) value);
 		} else if (value instanceof Number) {
-			if ((value instanceof Double || value instanceof Float) && null != styleSet && null != styleSet.getCellStyleForNumber()) {
+			if ((value instanceof Double || value instanceof Float  || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) {
 				cell.setCellStyle(styleSet.getCellStyleForNumber());
 			}
 			cell.setCellValue(((Number) value).doubleValue());