按照人们阅读Excel习惯来格式化BigDecimal

1、环境/问题描述

使用springboot发送邮件(附件)的方式将月度报表发送给领导查阅,数据是准确的,领导基本满意。

就是对一些数字的格式化提出了改进建议,比如不要让大数字自动转为科学计数法、浮点数小数点后都是0就不要带出来,根据某列的数值(0-100之间)设置单元格的底色,小于60的标深红色、大于等于60小于70标浅红色,大于等于70小于等于80标浅绿色,大于等于80小于90标中绿色,大于等于90的标深绿色。

2、分析/排查问题

1) 科学计算法的问题

经常,发现数字的长度超过8位Excel才会将次单元格中的值进行科学计算法转换,解决办法就是判断数字的长度,然后设置单元格存储的值类型

2) 浮点数小数点后都是0就不要带出来

数据表中的字段类型设置的是decimal(12,4),存储的值就是预期值,无小数位的就是证书、带小数位的小数位必定不全为0,只是通过mybatis-plus读取出来映射到Java Bean时自动将数字格式化为必定带4位小数,小数位无值则用0填充了

3) 根据某列的数值(0-100之间)设置单元格的底色

可以根据该单元格的数值大小根据规则进行设置色值,通过poi提供的方式给单元格设置底色

3、解决问题

1) 科学计算法的问题
java 复制代码
String s = o.toString();
// 当数字类型长度超过8位时,改为字符串类型显示(Excel数字超过一定长度会显示为科学计数法)
if ( isNumeric( o ) && s.length() < 8 ) {
	cell.setCellType( CellType.NUMERIC );
	cell.setCellValue( Double.parseDouble( s ) );
	return CELL_OTHER;
} else {
	cell.setCellType( CellType.STRING );
	cell.setCellValue( s );
}
2) 浮点数小数点后都是0就不要带出来
java 复制代码
public class BigDecimalFormatter {

    public static String format(BigDecimal number) {
        return format(number,5);
    }

    /**
     * 格式化BigDecimal
     * @param number 要处理的数字
     * @param newScale 保留的小数位
     * @return
     */
    public static String format(BigDecimal number,int newScale) {
        // 设置小数点后最多保留位位数(可以根据需要调整),并四舍五入
        BigDecimal formatted = number.setScale(newScale, RoundingMode.HALF_UP);

        // 再次移除尾随的零
        BigDecimal stripped = formatted.stripTrailingZeros();

        // 如果 scale 是负数或 0,则说明没有小数部分
        if (stripped.scale() <= 0) {
            return stripped.toBigInteger().toString(); // 返回整数部分
        } else {
            return stripped.toString(); // 包含小数部分
        }
    }

    public static String format2(BigDecimal number) {
        return number.stripTrailingZeros().toPlainString();
    }

    public static void main(String[] args) {
        System.out.println(format(new BigDecimal("123.000")));
        System.out.println(format(new BigDecimal("123.456")));
        System.out.println(format(new BigDecimal("123.00100")));
        System.out.println(format2(new BigDecimal("123.000")));
        System.out.println(format2(new BigDecimal("123.456")));
        System.out.println(format2(new BigDecimal("123.00100")));
    }
}
/**
 * 输出以下内容格式
 123
123.456
123.001
123
123.456
123.001
 */
3) 根据某列的数值(0-100之间)设置单元格的底色
java 复制代码
String hexColorStr = "十六进制色值";
XSSFColor xssfColor = new XSSFColor();
xssfColor.setARGBHex( hexColorStr );
cellStyle.setFillForegroundColor( xssfColor );
cellStyle.setFillBackgroundColor( xssfColor );
cellStyle.setFillPattern( FillPatternType.SOLID_FOREGROUND );
// 设置样式
cell.setCellStyle( cellStyle );

至此,问题得到解决,此处记录一下

相关推荐
奔波霸的伶俐虫2 小时前
navicat导出表结构到Excel 带字段备注
数据库·oracle·excel
出门喝奶茶8 小时前
通过 VBA 在 Excel 中自动提取拼音首字母
excel
guodashen00718 小时前
安卓使用JExcelApi读取Excel文件
android·excel·jexcelapi
后端小肥肠20 小时前
FastExcel + Java:打造高效灵活的Excel数据导入导出解决方案
java·开发语言·spring boot·后端·excel
三皮仔21 小时前
安装WPS后,导致python调用Excel.Application异常,解决办法
python·excel·wps
一路向北North2 天前
apache-poi导出excel数据
apache·excel
overmind2 天前
[oeasy]python064_命令行工作流的总结_vim_shell_python
python·vim·excel
hmywillstronger2 天前
【EXCEL】【VBA】查找sheet中小于阈值的值并提取单元格对应的行列对应编号(Distance Check查找距离过小的点对)
excel
默萧笙故2 天前
在 C# 中,处理 Excel 和 PDF 文件的库有很多。以下是一些比较常用的选择
pdf·excel
扎量丙不要犟2 天前
excel合并表格
excel