This commit is contained in:
Looly 2023-04-11 23:57:42 +08:00
parent 2df88aad56
commit 89ccae81d1
3 changed files with 26 additions and 63 deletions

View File

@ -307,16 +307,6 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
return this;
}
/**
* 设置 是否数字类型单元格精度根据单元格实际数值自动适配
* @param autoPrecision
* @return this
*/
public ExcelWriter setNumberAutoPrecision(boolean autoPrecision) {
this.styleSet.setNumberAutoPrecision(autoPrecision);
return this;
}
/**
* 获取样式集样式集可以自定义包括<br>
*

View File

@ -2,15 +2,7 @@ package cn.hutool.poi.excel;
import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import java.io.Serializable;
import java.math.BigDecimal;
@ -51,10 +43,6 @@ public class StyleSet implements Serializable {
*/
protected final CellStyle cellStyleForHyperlink;
/**
* 数字类型单元格精度根据单元格实际数值自动适配
*/
protected Boolean numberAutoPrecision;
/**
* 构造
*
@ -82,9 +70,6 @@ public class StyleSet implements Serializable {
font.setUnderline((byte) 1);
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
this.cellStyleForHyperlink.setFont(font);
// 数字类型单元格精度根据单元格实际数值自动适配
this.setNumberAutoPrecision(false);
}
/**
@ -133,14 +118,6 @@ public class StyleSet implements Serializable {
return this.cellStyleForHyperlink;
}
public Boolean getNumberAutoPrecision() {
return numberAutoPrecision;
}
public void setNumberAutoPrecision(Boolean numberAutoPrecision) {
this.numberAutoPrecision = numberAutoPrecision;
}
/**
* 定义所有单元格的边框类型
*
@ -269,13 +246,8 @@ public class StyleSet implements Serializable {
// 数字单独定义格式
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) &&
null != this.cellStyleForNumber) {
BigDecimal bigDecimalValue = new BigDecimal(value.toString());
if(numberAutoPrecision){
this.cellStyleForNumber.setDataFormat((short)bigDecimalValue.precision());
}else{
style = this.cellStyleForNumber;
}
}
} else if (value instanceof Hyperlink) {
// 自定义超链接样式
if (null != this.cellStyleForHyperlink) {

View File

@ -5,14 +5,14 @@ import lombok.Data;
import org.junit.Ignore;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* https://github.com/dromara/hutool/issues/3048 Excel导出javaBean中有BigDecimal类型精度流失
* https://github.com/dromara/hutool/issues/3048
* Excel导出javaBean中有BigDecimal类型精度流失
*
*/
public class Issue3048Test {
@ -22,11 +22,12 @@ public class Issue3048Test {
List<TestBean> excelExportList = new ArrayList<>();
excelExportList.add(new TestBean("1", new BigDecimal("1.22")));
excelExportList.add(new TestBean("2", new BigDecimal("2.342")));
excelExportList.add(new TestBean("3", new BigDecimal("1.2346")));
excelExportList.add(new TestBean("3", new BigDecimal("1.2346453453534534543545")));
ExcelWriter excelWriter = ExcelUtil.getWriter(true);
excelWriter.setNumberAutoPrecision(true);
//excelWriter.setNumberAutoPrecision(true);
excelWriter.write(excelExportList, true);
excelWriter.flush(new File("e:/test.xlsx"));
excelWriter.getStyleSet().getCellStyleForNumber().setDataFormat((short) 0);
excelWriter.flush(new File("d:/test/test.xlsx"));
excelWriter.close();
}