From 6e10a836473a5f208b9e7ef2eb08b72b06c38833 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 8 Aug 2024 11:10:25 +0800 Subject: [PATCH] add method --- .../dromara/hutool/poi/excel/ExcelReader.java | 1 + .../dromara/hutool/poi/excel/ExcelUtil.java | 2 + .../excel/{ => writer}/BigExcelWriter.java | 13 +- .../poi/excel/{ => writer}/ExcelWriter.java | 47 ++++---- .../hutool/poi/excel/writer/package-info.java | 19 +++ .../hutool/poi/excel/ExcelUtilTest.java | 1 + .../poi/excel/NumericCellValueTest.java | 1 + .../poi/excel/reader/Issue3481Test.java | 2 +- .../poi/excel/writer/BigExcelWriteTest.java | 4 +- .../poi/excel/writer/ExcelWriteBeanTest.java | 1 - .../poi/excel/writer/ExcelWriteTest.java | 113 +++++++++--------- .../poi/excel/writer/Issue2221Test.java | 3 +- .../poi/excel/writer/Issue2307Test.java | 1 - .../poi/excel/writer/IssueI66Z6BTest.java | 1 - .../poi/excel/writer/IssueI6MBS5Test.java | 1 - 15 files changed, 112 insertions(+), 98 deletions(-) rename hutool-poi/src/main/java/org/dromara/hutool/poi/excel/{ => writer}/BigExcelWriter.java (93%) rename hutool-poi/src/main/java/org/dromara/hutool/poi/excel/{ => writer}/ExcelWriter.java (97%) create mode 100644 hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/package-info.java diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelReader.java index 9a6c68f66..d4c430433 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelReader.java @@ -28,6 +28,7 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.dromara.hutool.poi.excel.writer.ExcelWriter; import java.io.File; import java.io.InputStream; diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelUtil.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelUtil.java index 5204cf218..02e192878 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelUtil.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelUtil.java @@ -20,6 +20,8 @@ import org.dromara.hutool.poi.PoiChecker; import org.dromara.hutool.poi.excel.sax.ExcelSaxReader; import org.dromara.hutool.poi.excel.sax.ExcelSaxUtil; import org.dromara.hutool.poi.excel.sax.handler.RowHandler; +import org.dromara.hutool.poi.excel.writer.BigExcelWriter; +import org.dromara.hutool.poi.excel.writer.ExcelWriter; import java.io.File; import java.io.InputStream; diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/BigExcelWriter.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/BigExcelWriter.java similarity index 93% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/BigExcelWriter.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/BigExcelWriter.java index 772e472bf..69ab51cfc 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/BigExcelWriter.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/BigExcelWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * Hutool is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -10,13 +10,14 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel; +package org.dromara.hutool.poi.excel.writer; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.io.IORuntimeException; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.dromara.hutool.poi.excel.WorkbookUtil; import java.io.File; import java.io.OutputStream; @@ -154,19 +155,19 @@ public class BigExcelWriter extends ExcelWriter { // -------------------------------------------------------------------------- Constructor end @Override - public BigExcelWriter autoSizeColumn(final int columnIndex) { + public BigExcelWriter autoSizeColumn(final int columnIndex, final boolean useMergedCells, final float widthRatio) { final SXSSFSheet sheet = (SXSSFSheet) this.sheet; sheet.trackColumnForAutoSizing(columnIndex); - super.autoSizeColumn(columnIndex); + super.autoSizeColumn(columnIndex, useMergedCells, widthRatio); sheet.untrackColumnForAutoSizing(columnIndex); return this; } @Override - public BigExcelWriter autoSizeColumnAll() { + public BigExcelWriter autoSizeColumnAll(final boolean useMergedCells, final float widthRatio) { final SXSSFSheet sheet = (SXSSFSheet) this.sheet; sheet.trackAllColumnsForAutoSizing(); - super.autoSizeColumnAll(); + super.autoSizeColumnAll(useMergedCells, widthRatio); sheet.untrackAllColumnsForAutoSizing(); return this; } diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelWriter.java similarity index 97% rename from hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelWriter.java rename to hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelWriter.java index ffe706a15..1d63b697b 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/ExcelWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 looly(loolly@aliyun.com) + * Copyright (c) 2024. looly(loolly@aliyun.com) * Hutool is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -10,7 +10,7 @@ * See the Mulan PSL v2 for more details. */ -package org.dromara.hutool.poi.excel; +package org.dromara.hutool.poi.excel.writer; import org.apache.poi.common.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.*; @@ -34,6 +34,9 @@ import org.dromara.hutool.core.map.multi.RowKeyTable; import org.dromara.hutool.core.map.multi.Table; import org.dromara.hutool.core.reflect.FieldUtil; import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.poi.excel.ExcelBase; +import org.dromara.hutool.poi.excel.RowUtil; +import org.dromara.hutool.poi.excel.WorkbookUtil; import org.dromara.hutool.poi.excel.cell.CellEditor; import org.dromara.hutool.poi.excel.cell.CellRangeUtil; import org.dromara.hutool.poi.excel.cell.CellUtil; @@ -262,41 +265,35 @@ public class ExcelWriter extends ExcelBase { * 此方法必须在指定列数据完全写出后调用才有效。
* 列数计算是通过第一行计算的 * + * @param useMergedCells 是否适用于合并单元格 + * @param widthRatio 列宽的倍数。如果所有内容都是英文,可以设为1,如果有中文,建议设置为 1.6-2.0之间。 * @return this * @since 4.0.12 */ - public ExcelWriter autoSizeColumnAll() { + public ExcelWriter autoSizeColumnAll(final boolean useMergedCells, final float widthRatio) { final int columnCount = this.getColumnCount(); for (int i = 0; i < columnCount; i++) { - autoSizeColumn(i); + autoSizeColumn(i, useMergedCells, widthRatio); } return this; } /** - * 设置某列为自动宽度,不考虑合并单元格
- * 此方法必须在指定列数据完全写出后调用才有效。 - * - * @param columnIndex 第几列,从0计数 - * @return this - * @since 4.0.12 - */ - public ExcelWriter autoSizeColumn(final int columnIndex) { - this.sheet.autoSizeColumn(columnIndex); - return this; - } - - /** - * 设置某列为自动宽度
+ * 设置某列为自动宽度。注意有中文的情况下,需要根据需求调整宽度扩大比例。
* 此方法必须在指定列数据完全写出后调用才有效。 * * @param columnIndex 第几列,从0计数 * @param useMergedCells 是否适用于合并单元格 + * @param widthRatio 列宽的倍数。如果所有内容都是英文,可以设为1,如果有中文,建议设置为 1.6-2.0之间。 * @return this - * @since 3.3.0 + * @since 5.8.30 */ - public ExcelWriter autoSizeColumn(final int columnIndex, final boolean useMergedCells) { - this.sheet.autoSizeColumn(columnIndex, useMergedCells); + public ExcelWriter autoSizeColumn(final int columnIndex, final boolean useMergedCells, final float widthRatio) { + if (widthRatio > 0) { + sheet.setColumnWidth(columnIndex, (int) (sheet.getColumnWidth(columnIndex) * widthRatio)); + } else { + sheet.autoSizeColumn(columnIndex, useMergedCells); + } return this; } @@ -562,10 +559,10 @@ public class ExcelWriter extends ExcelBase { if (sheet instanceof XSSFSheet) { ((XSSFSheet) sheet).addIgnoredErrors(cellRangeAddress, ignoredErrorTypes); return this; - } else if(sheet instanceof SXSSFSheet){ + } else if (sheet instanceof SXSSFSheet) { // SXSSFSheet并未提供忽略错误方法,获得其内部_sh字段设置 final XSSFSheet xssfSheet = (XSSFSheet) FieldUtil.getFieldValue(sheet, "_sh"); - if(null != xssfSheet){ + if (null != xssfSheet) { xssfSheet.addIgnoredErrors(cellRangeAddress, ignoredErrorTypes); } } @@ -694,8 +691,8 @@ public class ExcelWriter extends ExcelBase { * 指定样式传入null,则不使用任何样式 * * @param cellRangeAddress 合并单元格范围,定义了起始行列和结束行列 - * @param content 合并单元格后的内容 - * @param cellStyle 合并后单元格使用的样式,可以为null + * @param content 合并单元格后的内容 + * @param cellStyle 合并后单元格使用的样式,可以为null * @return this * @since 5.6.5 */ diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/package-info.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/package-info.java new file mode 100644 index 000000000..b217cfa6a --- /dev/null +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/writer/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2024. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +/** + * Excel生成封装 + * + * @author Looly + * @since 6.0.0 + */ +package org.dromara.hutool.poi.excel.writer; diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelUtilTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelUtilTest.java index 5f9bfc14d..4526ff5de 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelUtilTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/ExcelUtilTest.java @@ -14,6 +14,7 @@ package org.dromara.hutool.poi.excel; import org.apache.poi.ss.util.CellReference; import org.dromara.hutool.poi.excel.cell.CellReferenceUtil; +import org.dromara.hutool.poi.excel.writer.ExcelWriter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/NumericCellValueTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/NumericCellValueTest.java index 9a04ab305..67c894fd1 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/NumericCellValueTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/NumericCellValueTest.java @@ -14,6 +14,7 @@ package org.dromara.hutool.poi.excel; import org.apache.poi.ss.usermodel.Cell; import org.dromara.hutool.poi.excel.cell.values.NumericCellValue; +import org.dromara.hutool.poi.excel.writer.ExcelWriter; import org.junit.jupiter.api.Test; import java.util.Date; diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/Issue3481Test.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/Issue3481Test.java index 43452d00b..930afe9c6 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/Issue3481Test.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/Issue3481Test.java @@ -2,7 +2,7 @@ package org.dromara.hutool.poi.excel.reader; import org.dromara.hutool.poi.excel.ExcelReader; import org.dromara.hutool.poi.excel.ExcelUtil; -import org.dromara.hutool.poi.excel.ExcelWriter; +import org.dromara.hutool.poi.excel.writer.ExcelWriter; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/BigExcelWriteTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/BigExcelWriteTest.java index 96044464c..db400fc72 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/BigExcelWriteTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/BigExcelWriteTest.java @@ -22,9 +22,7 @@ import org.dromara.hutool.core.date.DateUtil; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.util.ObjUtil; -import org.dromara.hutool.poi.excel.BigExcelWriter; import org.dromara.hutool.poi.excel.ExcelUtil; -import org.dromara.hutool.poi.excel.ExcelWriter; import org.dromara.hutool.poi.excel.TestBean; import org.dromara.hutool.poi.excel.style.DefaultStyleSet; import org.dromara.hutool.poi.excel.style.StyleUtil; @@ -256,7 +254,7 @@ public class BigExcelWriteTest { put("userName", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); }}); writer.write(list, true); - writer.autoSizeColumnAll(); + writer.autoSizeColumnAll(false, 0); writer.close(); } } diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteBeanTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteBeanTest.java index 26d049109..0480cea30 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteBeanTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteBeanTest.java @@ -2,7 +2,6 @@ package org.dromara.hutool.poi.excel.writer; import lombok.Getter; import org.dromara.hutool.poi.excel.ExcelUtil; -import org.dromara.hutool.poi.excel.ExcelWriter; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java index b5e78ec67..0dda8c7ca 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/ExcelWriteTest.java @@ -12,12 +12,16 @@ package org.dromara.hutool.poi.excel.writer; +import org.apache.poi.common.usermodel.HyperlinkType; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.CellRangeAddressList; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.dromara.hutool.core.collection.ListUtil; +import org.dromara.hutool.core.data.id.IdUtil; import org.dromara.hutool.core.date.DateUtil; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.lang.Console; -import org.dromara.hutool.core.data.id.IdUtil; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.util.CharsetUtil; import org.dromara.hutool.core.util.ObjUtil; @@ -25,10 +29,6 @@ import org.dromara.hutool.poi.excel.*; import org.dromara.hutool.poi.excel.cell.setters.EscapeStrCellSetter; import org.dromara.hutool.poi.excel.style.DefaultStyleSet; import org.dromara.hutool.poi.excel.style.StyleUtil; -import org.apache.poi.common.usermodel.HyperlinkType; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.util.CellRangeAddressList; -import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -43,6 +43,23 @@ import java.util.*; */ public class ExcelWriteTest { + public static Map MAP_DATA_1 = new LinkedHashMap<>(); + public static Map MAP_DATA_2 = new LinkedHashMap<>(); + + static { + MAP_DATA_1.put("姓名", "张三"); + MAP_DATA_1.put("年龄", 23); + MAP_DATA_1.put("成绩", 88.32); + MAP_DATA_1.put("是否合格", true); + MAP_DATA_1.put("考试日期", DateUtil.now()); + + MAP_DATA_2.put("姓名", "张三"); + MAP_DATA_2.put("年龄", 23); + MAP_DATA_2.put("成绩", 88.32); + MAP_DATA_2.put("是否合格", true); + MAP_DATA_2.put("考试日期", DateUtil.now()); + } + @Test public void writeNoFlushTest() { final List row1 = ListUtil.of("aaaaa", "bb", "cc", "dd", DateUtil.now(), 3.22676575765); @@ -158,7 +175,7 @@ public class ExcelWriteTest { writer.merge(row1.size() - 1, "测试标题"); // 一次性写出内容,使用默认样式 writer.write(rows); - writer.autoSizeColumn(0, true); + writer.autoSizeColumn(0, true, 0); //冻结前两行 writer.setFreezePane(0, 2); // 关闭writer,释放内存 @@ -198,26 +215,12 @@ public class ExcelWriteTest { @Test @Disabled public void mergeTest2() { - final Map row1 = new LinkedHashMap<>(); - row1.put("姓名", "张三"); - row1.put("年龄", 23); - row1.put("成绩", 88.32); - row1.put("是否合格", true); - row1.put("考试日期", DateUtil.now()); - - final Map row2 = new LinkedHashMap<>(); - row2.put("姓名", "李四"); - row2.put("年龄", 33); - row2.put("成绩", 59.50); - row2.put("是否合格", false); - row2.put("考试日期", DateUtil.now()); - - final ArrayList> rows = ListUtil.of(row1, row2); + final ArrayList> rows = ListUtil.of(MAP_DATA_1, MAP_DATA_2); // 通过工具类创建writer final ExcelWriter writer = ExcelUtil.getWriter("d:/test/writeMapTest.xlsx"); // 合并单元格后的标题行,使用默认标题样式 - writer.merge(row1.size() - 1, "一班成绩单"); + writer.merge(MAP_DATA_1.size() - 1, "一班成绩单"); // 一次性写出内容,使用默认样式,强制输出标题 writer.write(rows, true); @@ -228,21 +231,7 @@ public class ExcelWriteTest { @Test @Disabled public void writeMapTest() { - final Map row1 = new LinkedHashMap<>(); - row1.put("姓名", "张三"); - row1.put("年龄", 23); - row1.put("成绩", 88.32); - row1.put("是否合格", true); - row1.put("考试日期", DateUtil.now()); - - final Map row2 = new LinkedHashMap<>(); - row2.put("姓名", "李四"); - row2.put("年龄", 33); - row2.put("成绩", 59.50); - row2.put("是否合格", false); - row2.put("考试日期", DateUtil.now()); - - final ArrayList> rows = ListUtil.of(row1, row2); + final ArrayList> rows = ListUtil.of(MAP_DATA_1, MAP_DATA_2); // 通过工具类创建writer final ExcelWriter writer = ExcelUtil.getWriter("e:/excel/writeMapTest.xlsx"); @@ -255,7 +244,7 @@ public class ExcelWriteTest { ((DefaultStyleSet)writer.getStyleSet()).setFont(font, true); // 合并单元格后的标题行,使用默认标题样式 - writer.merge(row1.size() - 1, "一班成绩单"); + writer.merge(MAP_DATA_1.size() - 1, "一班成绩单"); // 一次性写出内容,使用默认样式 writer.write(rows, true); // 关闭writer,释放内存 @@ -265,18 +254,11 @@ public class ExcelWriteTest { @Test @Disabled public void writeMapTest2() { - final Map row1 = MapUtil.newHashMap(true); - row1.put("姓名", "张三"); - row1.put("年龄", 23); - row1.put("成绩", 88.32); - row1.put("是否合格", true); - row1.put("考试日期", DateUtil.now()); - // 通过工具类创建writer final ExcelWriter writer = ExcelUtil.getWriter("e:/writeMapTest2.xlsx"); // 一次性写出内容,使用默认样式 - writer.writeRow(row1, true); + writer.writeRow(MAP_DATA_1, true); // 关闭writer,释放内存 writer.close(); } @@ -284,13 +266,6 @@ public class ExcelWriteTest { @Test @Disabled public void writeMapWithStyleTest() { - final Map row1 = MapUtil.newHashMap(true); - row1.put("姓名", "张三"); - row1.put("年龄", 23); - row1.put("成绩", 88.32); - row1.put("是否合格", true); - row1.put("考试日期", DateUtil.now()); - // 通过工具类创建writer final String path = "f:/test/writeMapWithStyleTest.xlsx"; FileUtil.del(FileUtil.file(path)); @@ -298,7 +273,7 @@ public class ExcelWriteTest { writer.setStyleSet(null); // 一次性写出内容,使用默认样式 - writer.writeRow(row1, true); + writer.writeRow(MAP_DATA_1, true); // 设置某个单元格样式 final CellStyle orCreateRowStyle = writer.getOrCreateCellStyle(0, 1); @@ -558,7 +533,7 @@ public class ExcelWriteTest { writer.setOnlyAlias(true); writer.write(rows, true); - writer.autoSizeColumnAll(); + writer.autoSizeColumnAll(false, 0); //表2 writer.setSheet("当前重复数据"); @@ -566,12 +541,12 @@ public class ExcelWriteTest { writer.addHeaderAlias("3", "行3"); writer.addHeaderAlias("1", "行1"); writer.write(rows, true); - writer.autoSizeColumnAll(); + writer.autoSizeColumnAll(false, 0); //表3 writer.setSheet("历史重复数据"); writer.write(rows, true); - writer.autoSizeColumnAll(); + writer.autoSizeColumnAll(false, 0); writer.close(); } @@ -896,4 +871,28 @@ public class ExcelWriteTest { final String disposition = writer.getDisposition("测试A12.xlsx", CharsetUtil.UTF_8); Assertions.assertEquals("attachment; filename=\"%E6%B5%8B%E8%AF%95A12.xlsx\"", disposition); } + + @Test + @Disabled + public void autoSizeColumnTest() { + + final Map map = new LinkedHashMap<>(MAP_DATA_1); + map.put("中文长度测试(符号)", "abc"); + + final String file1 = "d:/test/autoSizeColumnTest.xlsx"; + final String file2 = "d:/test/autoSizeColumnTest2.xlsx"; + + FileUtil.del(file1); + FileUtil.del(file2); + + try (final ExcelWriter writer = new ExcelWriter(file1)) { + writer.writeRow(map, true); + writer.autoSizeColumnAll(false, 2f); + } + + try (final BigExcelWriter writer = new BigExcelWriter(file2)) { + writer.writeRow(map, true); + writer.autoSizeColumnAll(false, 2f); + } + } } diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2221Test.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2221Test.java index e52458e4a..3602d5f81 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2221Test.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2221Test.java @@ -19,7 +19,6 @@ import org.apache.poi.ss.util.CellRangeAddress; import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.poi.excel.ExcelUtil; -import org.dromara.hutool.poi.excel.ExcelWriter; import org.dromara.hutool.poi.excel.style.DefaultStyleSet; import org.dromara.hutool.poi.excel.style.StyleUtil; import org.junit.jupiter.api.Disabled; @@ -107,7 +106,7 @@ public class Issue2221Test { ); // 自动尺寸 - writer.autoSizeColumnAll(); + writer.autoSizeColumnAll(false, 0); writer.write(data, true); writer.close(); diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2307Test.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2307Test.java index 0788581ee..cef32d6bc 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2307Test.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/Issue2307Test.java @@ -19,7 +19,6 @@ import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.poi.excel.ExcelUtil; -import org.dromara.hutool.poi.excel.ExcelWriter; import org.dromara.hutool.poi.excel.style.DefaultStyleSet; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI66Z6BTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI66Z6BTest.java index c19549228..7c69b5435 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI66Z6BTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI66Z6BTest.java @@ -15,7 +15,6 @@ package org.dromara.hutool.poi.excel.writer; import org.dromara.hutool.core.date.DateUtil; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.poi.excel.ExcelUtil; -import org.dromara.hutool.poi.excel.ExcelWriter; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI6MBS5Test.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI6MBS5Test.java index c3f137cab..f66dee6bb 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI6MBS5Test.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/writer/IssueI6MBS5Test.java @@ -14,7 +14,6 @@ package org.dromara.hutool.poi.excel.writer; import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.poi.excel.ExcelUtil; -import org.dromara.hutool.poi.excel.ExcelWriter; import org.dromara.hutool.poi.excel.WorkbookUtil; import org.dromara.hutool.poi.excel.cell.CellUtil; import org.apache.poi.ss.usermodel.Cell;