Pre Merge pull request !587 from 长江游泳鱼/master

This commit is contained in:
长江游泳鱼 2026-06-10 03:34:14 +00:00 committed by Gitee
commit 453ffb795c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -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;
@ -1145,11 +1146,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())
@ -1288,7 +1313,53 @@ public class ExcelUtil<T>
}
else if (value instanceof BigDecimal && -1 != attr.scale())
{
cell.setCellValue((((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))
{