1.修复Excel导出的数值型列,当指定了保留小数位数(scale)时,会因Excel把数字渲染成日期,而错误显示成日期的问题。
2.增加数值型(包括int、long、double、BigInteger、BigDecimal等型)列的千分位逗号格式化,包括小数的类型(double、BigDecimal等)根据scale指定的小数位数显示
This commit is contained in:
parent
6a7c448d55
commit
4e1d0dd30a
|
|
@ -9,6 +9,7 @@ import java.lang.reflect.Field;
|
|||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
|
@ -1029,11 +1030,35 @@ public class ExcelUtil<T>
|
|||
}
|
||||
cell.setCellValue(StringUtils.isNull(cellValue) ? attr.defaultValue() : cellValue + attr.suffix());
|
||||
}
|
||||
else if (ColumnType.NUMERIC == attr.cellType())
|
||||
else if (ColumnType.NUMERIC == attr.cellType()) // String型但却又cellType为NUMERIC,则将字符串转为数字
|
||||
{
|
||||
if (StringUtils.isNotNull(value))
|
||||
{
|
||||
cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
|
||||
String numStr = Convert.toStr(value);
|
||||
boolean hasDecimal = StringUtils.contains(numStr, ".");
|
||||
|
||||
if (hasDecimal)
|
||||
{
|
||||
Double doubleVal = Convert.toDouble(value);
|
||||
int scale = attr.scale();
|
||||
StringBuilder patternBuilder = new StringBuilder("#,##0");
|
||||
if (scale >= 0)
|
||||
{
|
||||
patternBuilder.append('.');
|
||||
for (int i = 0; i < scale; i++)
|
||||
{
|
||||
patternBuilder.append('0');
|
||||
}
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(patternBuilder.toString());
|
||||
cell.setCellValue(df.format(doubleVal));
|
||||
}
|
||||
else
|
||||
{
|
||||
Long longVal = Convert.toLong(value);
|
||||
DecimalFormat df = new DecimalFormat("#,##0");
|
||||
cell.setCellValue(df.format(longVal));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (ColumnType.IMAGE == attr.cellType())
|
||||
|
|
@ -1171,7 +1196,53 @@ public class ExcelUtil<T>
|
|||
}
|
||||
else if (value instanceof BigDecimal && -1 != attr.scale())
|
||||
{
|
||||
cell.setCellValue(Convert.toStr((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).doubleValue()));
|
||||
//数值格式化
|
||||
BigDecimal bdValue = (BigDecimal) value;
|
||||
BigDecimal scaledValue = bdValue.setScale(attr.scale(), attr.roundingMode());
|
||||
StringBuilder patternBuilder = new StringBuilder("#,##0");
|
||||
if (attr.scale() > 0)
|
||||
{
|
||||
patternBuilder.append('.');
|
||||
}
|
||||
for (int i = 0; i < attr.scale(); i++)
|
||||
{
|
||||
patternBuilder.append('0');
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(patternBuilder.toString());
|
||||
cell.setCellValue(df.format(scaledValue));
|
||||
}
|
||||
else if (value instanceof BigInteger)
|
||||
{
|
||||
BigInteger bigIntValue = (BigInteger) value;
|
||||
DecimalFormat df = new DecimalFormat("#,##0");
|
||||
cell.setCellValue(df.format(bigIntValue));
|
||||
}
|
||||
else if (value instanceof Number && !(value instanceof BigDecimal))
|
||||
{
|
||||
Number numValue = (Number) value;
|
||||
String numStr = numValue.toString();
|
||||
boolean hasDecimal = StringUtils.contains(numStr, ".");
|
||||
|
||||
if (hasDecimal)
|
||||
{
|
||||
int scale = attr.scale();
|
||||
StringBuilder patternBuilder = new StringBuilder("#,##0");
|
||||
if (scale >= 0)
|
||||
{
|
||||
patternBuilder.append('.');
|
||||
for (int i = 0; i < scale; i++)
|
||||
{
|
||||
patternBuilder.append('0');
|
||||
}
|
||||
}
|
||||
DecimalFormat df = new DecimalFormat(patternBuilder.toString());
|
||||
cell.setCellValue(df.format(numValue.doubleValue()));
|
||||
}
|
||||
else
|
||||
{
|
||||
DecimalFormat df = new DecimalFormat("#,##0");
|
||||
cell.setCellValue(df.format(numValue.longValue()));
|
||||
}
|
||||
}
|
||||
else if (!attr.handler().equals(ExcelHandlerAdapter.class))
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user