1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00
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
hutool-poi/src
main/java/cn/hutool/poi/excel
test/java/cn/hutool/poi/excel

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>
*
@ -899,7 +889,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
public ExcelWriter writeImg(File imgFile, int imgType, int dx1, int dy1, int dx2,
int dy2, int col1, int row1, int col2, int row2) {
return writeImg(FileUtil.readBytes(imgFile), imgType, dx1,
dy1, dx2, dy2, col1, row1, col2, row2);
dy1, dx2, dy2, col1, row1, col2, row2);
}
/**
@ -907,15 +897,15 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* 添加图片到当前sheet中
*
* @param pictureData 数据bytes
* @param imgType 图片类型对应poi中Workbook类中的图片类型2-7变量
* @param dx1 起始单元格中的x坐标
* @param dy1 起始单元格中的y坐标
* @param dx2 结束单元格中的x坐标
* @param dy2 结束单元格中的y坐标
* @param col1 指定起始的列下标从0开始
* @param row1 指定起始的行下标从0开始
* @param col2 指定结束的列下标从0开始
* @param row2 指定结束的行下标从0开始
* @param imgType 图片类型对应poi中Workbook类中的图片类型2-7变量
* @param dx1 起始单元格中的x坐标
* @param dy1 起始单元格中的y坐标
* @param dx2 结束单元格中的x坐标
* @param dy2 结束单元格中的y坐标
* @param col1 指定起始的列下标从0开始
* @param row1 指定起始的行下标从0开始
* @param col2 指定结束的列下标从0开始
* @param row2 指定结束的行下标从0开始
* @return this
* @author vhukze
* @since 5.8.0
@ -1022,7 +1012,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
} else {
rowMap = (Map) rowBean;
}
}else if(rowBean instanceof Iterable){
} else if (rowBean instanceof Iterable) {
// issue#2398@Github
// MapWrapper由于实现了Iterable接口应该优先按照Map处理
return writeRow((Iterable<?>) rowBean);
@ -1077,7 +1067,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
for (Table.Cell<?, ?, ?> cell : aliasTable) {
// 首先查找原名对应的列号
location = this.headLocationCache.get(StrUtil.toString(cell.getRowKey()));
if(null == location){
if (null == location) {
// 未找到则查找别名对应的列号
location = this.headLocationCache.get(StrUtil.toString(cell.getColumnKey()));
}
@ -1355,9 +1345,9 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
private Table<?, ?, ?> aliasTable(Map<?, ?> rowMap) {
final Table<Object, Object, Object> filteredTable = new RowKeyTable<>(new LinkedHashMap<>(), TableMap::new);
if (MapUtil.isEmpty(this.headerAlias)) {
rowMap.forEach((key, value)-> filteredTable.put(key, key, value));
}else{
rowMap.forEach((key, value)->{
rowMap.forEach((key, value) -> filteredTable.put(key, key, value));
} else {
rowMap.forEach((key, value) -> {
final String aliasName = this.headerAlias.get(StrUtil.toString(key));
if (null != aliasName) {
// 别名键值对加入

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;
}
/**
* 定义所有单元格的边框类型
*
@ -259,8 +236,8 @@ public class StyleSet implements Serializable {
}
if (value instanceof Date
|| value instanceof TemporalAccessor
|| value instanceof Calendar) {
|| value instanceof TemporalAccessor
|| value instanceof Calendar) {
// 日期单独定义格式
if (null != this.cellStyleForDate) {
style = this.cellStyleForDate;
@ -268,13 +245,8 @@ public class StyleSet implements Serializable {
} else if (value instanceof Number) {
// 数字单独定义格式
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;
}
null != this.cellStyleForNumber) {
style = this.cellStyleForNumber;
}
} else if (value instanceof Hyperlink) {
// 自定义超链接样式

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();
}