add method

This commit is contained in:
Looly 2024-08-08 11:10:25 +08:00
parent 6ba8f1be69
commit 6e10a83647
15 changed files with 112 additions and 98 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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<ExcelWriter> {
* 此方法必须在指定列数据完全写出后调用才有效<br>
* 列数计算是通过第一行计算的
*
* @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;
}
/**
* 设置某列为自动宽度不考虑合并单元格<br>
* 此方法必须在指定列数据完全写出后调用才有效
*
* @param columnIndex 第几列从0计数
* @return this
* @since 4.0.12
*/
public ExcelWriter autoSizeColumn(final int columnIndex) {
this.sheet.autoSizeColumn(columnIndex);
return this;
}
/**
* 设置某列为自动宽度<br>
* 设置某列为自动宽度注意有中文的情况下需要根据需求调整宽度扩大比例<br>
* 此方法必须在指定列数据完全写出后调用才有效
*
* @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<ExcelWriter> {
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<ExcelWriter> {
* 指定样式传入null则不使用任何样式
*
* @param cellRangeAddress 合并单元格范围定义了起始行列和结束行列
* @param content 合并单元格后的内容
* @param cellStyle 合并后单元格使用的样式可以为null
* @param content 合并单元格后的内容
* @param cellStyle 合并后单元格使用的样式可以为null
* @return this
* @since 5.6.5
*/

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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<String, Object> MAP_DATA_1 = new LinkedHashMap<>();
public static Map<String, Object> 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<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.now());
final Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.now());
final ArrayList<Map<String, Object>> rows = ListUtil.of(row1, row2);
final ArrayList<Map<String, Object>> 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<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.now());
final Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.now());
final ArrayList<Map<String, Object>> rows = ListUtil.of(row1, row2);
final ArrayList<Map<String, Object>> 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<String, Object> 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<String, Object> 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<String, Object> 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);
}
}
}

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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;