mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 20:58:00 +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>
|
||||||
*
|
*
|
||||||
@ -1022,7 +1012,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
|||||||
} else {
|
} else {
|
||||||
rowMap = (Map) rowBean;
|
rowMap = (Map) rowBean;
|
||||||
}
|
}
|
||||||
}else if(rowBean instanceof Iterable){
|
} else if (rowBean instanceof Iterable) {
|
||||||
// issue#2398@Github
|
// issue#2398@Github
|
||||||
// MapWrapper由于实现了Iterable接口,应该优先按照Map处理
|
// MapWrapper由于实现了Iterable接口,应该优先按照Map处理
|
||||||
return writeRow((Iterable<?>) rowBean);
|
return writeRow((Iterable<?>) rowBean);
|
||||||
@ -1077,7 +1067,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
|||||||
for (Table.Cell<?, ?, ?> cell : aliasTable) {
|
for (Table.Cell<?, ?, ?> cell : aliasTable) {
|
||||||
// 首先查找原名对应的列号
|
// 首先查找原名对应的列号
|
||||||
location = this.headLocationCache.get(StrUtil.toString(cell.getRowKey()));
|
location = this.headLocationCache.get(StrUtil.toString(cell.getRowKey()));
|
||||||
if(null == location){
|
if (null == location) {
|
||||||
// 未找到,则查找别名对应的列号
|
// 未找到,则查找别名对应的列号
|
||||||
location = this.headLocationCache.get(StrUtil.toString(cell.getColumnKey()));
|
location = this.headLocationCache.get(StrUtil.toString(cell.getColumnKey()));
|
||||||
}
|
}
|
||||||
@ -1355,9 +1345,9 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
|
|||||||
private Table<?, ?, ?> aliasTable(Map<?, ?> rowMap) {
|
private Table<?, ?, ?> aliasTable(Map<?, ?> rowMap) {
|
||||||
final Table<Object, Object, Object> filteredTable = new RowKeyTable<>(new LinkedHashMap<>(), TableMap::new);
|
final Table<Object, Object, Object> filteredTable = new RowKeyTable<>(new LinkedHashMap<>(), TableMap::new);
|
||||||
if (MapUtil.isEmpty(this.headerAlias)) {
|
if (MapUtil.isEmpty(this.headerAlias)) {
|
||||||
rowMap.forEach((key, value)-> filteredTable.put(key, key, value));
|
rowMap.forEach((key, value) -> filteredTable.put(key, key, value));
|
||||||
}else{
|
} else {
|
||||||
rowMap.forEach((key, value)->{
|
rowMap.forEach((key, value) -> {
|
||||||
final String aliasName = this.headerAlias.get(StrUtil.toString(key));
|
final String aliasName = this.headerAlias.get(StrUtil.toString(key));
|
||||||
if (null != aliasName) {
|
if (null != aliasName) {
|
||||||
// 别名键值对加入
|
// 别名键值对加入
|
||||||
|
@ -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