按照人们阅读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 );

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

相关推荐
2601_962300815 小时前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
ms365copilot6 小时前
Excel Copilot一键美化图表,无需手动调格式
excel·copilot·outlook
2501_933670796 小时前
内部审计校招的数据化趋势:SQL、Excel、Power BI和证书选择
数据库·sql·excel
weixin_493503671 天前
商会系统里的在线文件预览:PDF/Word/Excel 三种实现与选型(Node + 前端实战)
pdf·word·excel
asdzx671 天前
Python 解析 Excel 数据、图片与图表的实现方案
python·excel
慧都小妮子1 天前
GcExcel 服务端 Excel 组件:无需安装 Office 的 Java/.NET 报表方案
java·.net·excel·gcexcel·服务端excel组件
The Future is mine2 天前
Excel如何只允许对部分表格内容进行修改
excel
2601_962099462 天前
Python xlwt设置excel单元格字体及格式
python·excel·xlwt·样式设置·单元格格式
葡萄城技术团队2 天前
不会写 SUMIFS 也能做汇总?用 SpreadJS AI 生成并解释 Excel 公式
人工智能·excel
2501_933670792 天前
采购运营校招Excel能力清单:函数、透视表、ERP数据与SQL入门
数据库·sql·excel