mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
回退
This commit is contained in:
parent
2df88aad56
commit
89ccae81d1
@ -307,16 +307,6 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置 是否数字类型单元格精度根据单元格实际数值自动适配
|
|
||||||
* @param autoPrecision
|
|
||||||
* @return this
|
|
||||||
*/
|
|
||||||
public ExcelWriter setNumberAutoPrecision(boolean autoPrecision) {
|
|
||||||
this.styleSet.setNumberAutoPrecision(autoPrecision);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取样式集,样式集可以自定义包括:<br>
|
* 获取样式集,样式集可以自定义包括:<br>
|
||||||
*
|
*
|
||||||
|
@ -2,15 +2,7 @@ package cn.hutool.poi.excel;
|
|||||||
|
|
||||||
import cn.hutool.poi.excel.style.StyleUtil;
|
import cn.hutool.poi.excel.style.StyleUtil;
|
||||||
import org.apache.poi.hssf.util.HSSFColor;
|
import org.apache.poi.hssf.util.HSSFColor;
|
||||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
import org.apache.poi.ss.usermodel.*;
|
||||||
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 java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
@ -51,10 +43,6 @@ public class StyleSet implements Serializable {
|
|||||||
*/
|
*/
|
||||||
protected final CellStyle cellStyleForHyperlink;
|
protected final CellStyle cellStyleForHyperlink;
|
||||||
|
|
||||||
/**
|
|
||||||
* 数字类型单元格精度根据单元格实际数值自动适配
|
|
||||||
*/
|
|
||||||
protected Boolean numberAutoPrecision;
|
|
||||||
/**
|
/**
|
||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
@ -82,9 +70,6 @@ public class StyleSet implements Serializable {
|
|||||||
font.setUnderline((byte) 1);
|
font.setUnderline((byte) 1);
|
||||||
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
|
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
|
||||||
this.cellStyleForHyperlink.setFont(font);
|
this.cellStyleForHyperlink.setFont(font);
|
||||||
|
|
||||||
// 数字类型单元格精度根据单元格实际数值自动适配
|
|
||||||
this.setNumberAutoPrecision(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,14 +118,6 @@ public class StyleSet implements Serializable {
|
|||||||
return this.cellStyleForHyperlink;
|
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) &&
|
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) &&
|
||||||
null != this.cellStyleForNumber) {
|
null != this.cellStyleForNumber) {
|
||||||
BigDecimal bigDecimalValue = new BigDecimal(value.toString());
|
|
||||||
if(numberAutoPrecision){
|
|
||||||
this.cellStyleForNumber.setDataFormat((short)bigDecimalValue.precision());
|
|
||||||
}else{
|
|
||||||
style = this.cellStyleForNumber;
|
style = this.cellStyleForNumber;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else if (value instanceof Hyperlink) {
|
} else if (value instanceof Hyperlink) {
|
||||||
// 自定义超链接样式
|
// 自定义超链接样式
|
||||||
if (null != this.cellStyleForHyperlink) {
|
if (null != this.cellStyleForHyperlink) {
|
||||||
|
@ -5,14 +5,14 @@ import lombok.Data;
|
|||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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 {
|
public class Issue3048Test {
|
||||||
@ -22,11 +22,12 @@ public class Issue3048Test {
|
|||||||
List<TestBean> excelExportList = new ArrayList<>();
|
List<TestBean> excelExportList = new ArrayList<>();
|
||||||
excelExportList.add(new TestBean("1", new BigDecimal("1.22")));
|
excelExportList.add(new TestBean("1", new BigDecimal("1.22")));
|
||||||
excelExportList.add(new TestBean("2", new BigDecimal("2.342")));
|
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 excelWriter = ExcelUtil.getWriter(true);
|
||||||
excelWriter.setNumberAutoPrecision(true);
|
//excelWriter.setNumberAutoPrecision(true);
|
||||||
excelWriter.write(excelExportList, 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();
|
excelWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user