最近搜索

Excel小数点数字。存到数据库 如果存成文本。精度问题

浏览:529
管理员 2022-02-22 03:23
for (int rowNum =1; rowNum <= sheet.getLastRowNum(); rowNum++) {
    Row row = sheet.getRow(rowNum);
    if (row == null) {
        continue;
    }
    String bianhao = ExcelUtil.formatCell(row.getCell(0)).trim();
    String biaozhun = ExcelUtil.formatCell(row.getCell(1)).trim();
    String guigge = ExcelUtil.formatCell(row.getCell(2)).trim();
    String price = ExcelUtil.formatCell(row.getCell(3)).trim();
    System.out.println(bianhao + "__" + biaozhun + "__" + guigge + "__" + price   );

输出结果如下:

120__HG5028-58__BL250-10__141

120__HG5028-58__BL250-10__141

121__HG5028-58__BL250-16__142

121__HG5028-58__BL250-16__142

122__HG5028-58__BL300-10__143

122__HG5028-58__BL300-10__143

123__HG5028-58__BL300-16__144

123__HG5028-58__BL300-16__144

124__HG5028-58__BL350-10__145

124__HG5028-58__BL350-10__145

125__HG5028-58__BL350-16__146

125__HG5028-58__BL350-16__146

126__HG5028-58__BL400-10__147

126__HG5028-58__BL400-10__147

127__HG5028-58__BL400-16__148

excel真实数据是:

image.png

可以看出来,读的数值没有小数点了。



换一种读的方式:

String bianhao = ExcelUtil.formatCell(row.getCell(0)).trim();
String biaozhun = ExcelUtil.formatCell(row.getCell(1)).trim();
String guigge = ExcelUtil.formatCell(row.getCell(2)).trim();
String price = ExcelUtil.formatCell(row.getCell(3)).trim();
BigDecimal lim2 = ExcelUtil.formatBigDecimal(row.getCell(3));
System.out.println(bianhao + "__" + biaozhun + "__" + guigge + "__" + price  +"_______"+lim2 );

image.png


excel表,并没有单独设置什么,就是原样。没有做这个操作(右击设置成文本)

如果右击设置成文本也没有影响结果和上图一样。














这个检测结果存的是文本。(不存bigdecimal),这种方式不合理,因为精度发生了变化。

精度如下图。

String  result = String.valueOf(row.getCell(x).getStringCellValue());  
直接拿文本这种不行。

BigDecimal result = ExcelUtil.formatBigDecimal(row.getCell(x));
拿bigdecimal 再转成文本(result.toString())也不行

image.png




直接读取文本直接存。精度丢失

image.png

image.png


读取方法

String workDate_text  = ExcelUtil.formatCell(row.getCell(10)).trim();
String dept  = ExcelUtil.formatCell(row.getCell(11)).trim();
String zu  = ExcelUtil.formatCell(row.getCell(12)).trim();					
					

image.png





合理的存方法是bigdecimal不存文本。


BigDecimal result = ExcelUtil.formatBigDecimal(row.getCell(x));
存bigdecimal







联系站长

站长微信:xiaomao0055

站长QQ:14496453