异常代码
public class Test5 {
public static void main(String[] args) {
String fileName = "g://" + "demo" + File.separator + "demo.xlsx";
List<DemoData> list = EasyExcel.read(fileName, DemoData.class, null).sheet().doReadSync();
List<DemoData1> result = new ArrayList<>();
for(DemoData demoData:list){
if(demoData.getCol2() != null){
demoData.setCol2(demoData.getCol2().add(new BigDecimal("500000000000.123456")));
DemoData1 demoData1= new DemoData1();
demoData1.setCol1(demoData.getCol1());
demoData1.setCol2(demoData.getCol2().toString());
result.add(demoData1);
}
}
//case1
EasyExcel.write("g://" + "demo" + File.separator + "demoTemp1.xlsx",DemoData.class).sheet().doWrite(list);
//case2
EasyExcel.write("g://" + "demo" + File.separator + "demoTemp2.xlsx",DemoData1.class).sheet().doWrite(result);
}
}
public class DemoData {
@ExcelProperty("序号")
private String col1;
@ExcelProperty("数字1")
private BigDecimal col2;
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public BigDecimal getCol2() {
return col2;
}
public void setCol2(BigDecimal col2) {
this.col2 = col2;
}
}
public class DemoData1 {
@ExcelProperty("序号")
private String col1;
@ExcelProperty("数字1")
private String col2;
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
}
异常提示 1、导入文件格式如下图,可以看到展现的值与实际的值不一样,为了读取实际值我采用BigDecimal 去读 2、经过处理后,用同样的dto去导出,如下图,可以看到后面精度已丢失 3、为了解决这种问题,我采用切换dto的方式,将字段类型换成String去写入,导出结果如下图,可以看到这样结果是对了,但是涉及大量的dto copy,影响效率,而我如果采用String去读的话,又只会读取展现的值,读取的数据会丢失精度,请问对于这种场景有什么好的解决办法么?
建议描述