feat: Add widthRatio parameter for Excel autoSizeColumn

This commit is contained in:
bourne7 2024-08-07 20:02:07 +08:00
parent 02a38a4f52
commit fdf9d5dd5e
2 changed files with 150 additions and 137 deletions

View File

@ -30,12 +30,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Comparator; import java.util.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
@ -241,9 +236,22 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @since 4.0.12 * @since 4.0.12
*/ */
public ExcelWriter autoSizeColumnAll() { public ExcelWriter autoSizeColumnAll() {
return autoSizeColumnAll(0f);
}
/**
* 设置所有列为自动宽度不考虑合并单元格<br>
* 此方法必须在指定列数据完全写出后调用才有效<br>
* 列数计算是通过第一行计算的
*
* @param widthRatio 列宽的倍数如果所有内容都是英文可以设为1如果有中文建议设置为 1.6-2.0之间
* @return this
* @since 5.8.30
*/
public ExcelWriter autoSizeColumnAll(float widthRatio) {
final int columnCount = this.getColumnCount(); final int columnCount = this.getColumnCount();
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
autoSizeColumn(i); autoSizeColumn(i, widthRatio);
} }
return this; return this;
} }
@ -257,8 +265,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @since 4.0.12 * @since 4.0.12
*/ */
public ExcelWriter autoSizeColumn(int columnIndex) { public ExcelWriter autoSizeColumn(int columnIndex) {
this.sheet.autoSizeColumn(columnIndex); return autoSizeColumn(columnIndex, false);
return this;
} }
/** /**
@ -271,7 +278,37 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @since 3.3.0 * @since 3.3.0
*/ */
public ExcelWriter autoSizeColumn(int columnIndex, boolean useMergedCells) { public ExcelWriter autoSizeColumn(int columnIndex, boolean useMergedCells) {
this.sheet.autoSizeColumn(columnIndex, useMergedCells); return autoSizeColumn(columnIndex, useMergedCells, 0f);
}
/**
* 设置某列为自动宽度注意有中文的情况下需要根据需求调整宽度扩大比例<br>
* 此方法必须在指定列数据完全写出后调用才有效
*
* @param columnIndex 第几列从0计数
* @param widthRatio 列宽的倍数如果所有内容都是英文可以设为1如果有中文建议设置为 1.6-2.0之间
* @return this
* @since 5.8.30
*/
public ExcelWriter autoSizeColumn(int columnIndex, float widthRatio) {
return autoSizeColumn(columnIndex, false, widthRatio);
}
/**
* 设置某列为自动宽度注意有中文的情况下需要根据需求调整宽度扩大比例<br>
* 此方法必须在指定列数据完全写出后调用才有效
*
* @param columnIndex 第几列从0计数
* @param useMergedCells 是否适用于合并单元格
* @param widthRatio 列宽的倍数如果所有内容都是英文可以设为1如果有中文建议设置为 1.6-2.0之间
* @return this
* @since 5.8.30
*/
public ExcelWriter autoSizeColumn(int columnIndex, boolean useMergedCells, float widthRatio) {
sheet.autoSizeColumn(columnIndex, useMergedCells);
if (widthRatio > 0) {
sheet.setColumnWidth(columnIndex, (int) (sheet.getColumnWidth(columnIndex) * widthRatio));
}
return this; return this;
} }
@ -901,7 +938,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
public ExcelWriter writeImg(File imgFile, int imgType, int dx1, int dy1, int dx2, public ExcelWriter writeImg(File imgFile, int imgType, int dx1, int dy1, int dx2,
int dy2, int col1, int row1, int col2, int row2) { int dy2, int col1, int row1, int col2, int row2) {
return writeImg(FileUtil.readBytes(imgFile), imgType, dx1, return writeImg(FileUtil.readBytes(imgFile), imgType, dx1,
dy1, dx2, dy2, col1, row1, col2, row2); dy1, dx2, dy2, col1, row1, col2, row2);
} }
/** /**
@ -1118,7 +1155,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values * @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values
* @return this * @return this
*/ */
public ExcelWriter writeCol(Map<?,? extends Iterable<?>> colMap, boolean isWriteKeyAsHead){ public ExcelWriter writeCol(Map<?, ? extends Iterable<?>> colMap, boolean isWriteKeyAsHead) {
return writeCol(colMap, 0, isWriteKeyAsHead); return writeCol(colMap, 0, isWriteKeyAsHead);
} }
@ -1133,12 +1170,12 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values * @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values
* @return this * @return this
*/ */
public ExcelWriter writeCol(Map<?,? extends Iterable<?>> colMap, int startColIndex, boolean isWriteKeyAsHead){ public ExcelWriter writeCol(Map<?, ? extends Iterable<?>> colMap, int startColIndex, boolean isWriteKeyAsHead) {
for (Object k : colMap.keySet()) { for (Object k : colMap.keySet()) {
Iterable<?> v = colMap.get(k); Iterable<?> v = colMap.get(k);
if(v != null){ if (v != null) {
writeCol(isWriteKeyAsHead?k:null,startColIndex, v, startColIndex != colMap.size() - 1); writeCol(isWriteKeyAsHead ? k : null, startColIndex, v, startColIndex != colMap.size() - 1);
startColIndex ++; startColIndex++;
} }
} }
return this; return this;
@ -1156,8 +1193,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方 * @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方
* @return this * @return this
*/ */
public ExcelWriter writeCol(Object headerVal, Iterable<?> colData, boolean isResetRowIndex){ public ExcelWriter writeCol(Object headerVal, Iterable<?> colData, boolean isResetRowIndex) {
return writeCol(headerVal,0,colData,isResetRowIndex); return writeCol(headerVal, 0, colData, isResetRowIndex);
} }
/** /**
@ -1172,18 +1209,18 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方 * @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方
* @return this * @return this
*/ */
public ExcelWriter writeCol(Object headerVal, int colIndex, Iterable<?> colData, boolean isResetRowIndex){ public ExcelWriter writeCol(Object headerVal, int colIndex, Iterable<?> colData, boolean isResetRowIndex) {
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!"); Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
int currentRowIndex = currentRow.get(); int currentRowIndex = currentRow.get();
if(null != headerVal){ if (null != headerVal) {
writeCellValue(colIndex, currentRowIndex, headerVal,true); writeCellValue(colIndex, currentRowIndex, headerVal, true);
currentRowIndex++; currentRowIndex++;
} }
for (Object colDatum : colData) { for (Object colDatum : colData) {
writeCellValue(colIndex, currentRowIndex, colDatum); writeCellValue(colIndex, currentRowIndex, colDatum);
currentRowIndex++; currentRowIndex++;
} }
if(!isResetRowIndex){ if (!isResetRowIndex) {
currentRow.set(currentRowIndex); currentRow.set(currentRowIndex);
} }
return this; return this;
@ -1231,10 +1268,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/** /**
* 给指定单元格赋值使用默认单元格样式 * 给指定单元格赋值使用默认单元格样式
* *
* @param x X坐标从0计数即列号 * @param x X坐标从0计数即列号
* @param y Y坐标从0计数即行号 * @param y Y坐标从0计数即行号
* @param isHeader 是否为Header * @param isHeader 是否为Header
* @param value * @param value
* @return this * @return this
* @since 4.0.2 * @since 4.0.2
*/ */

View File

@ -12,15 +12,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.cell.setters.EscapeStrCellSetter; import cn.hutool.poi.excel.cell.setters.EscapeStrCellSetter;
import cn.hutool.poi.excel.style.StyleUtil; import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.BuiltinFormats;
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.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.junit.Assert; import org.junit.Assert;
@ -28,15 +20,7 @@ import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/** /**
* 写出Excel单元测试 * 写出Excel单元测试
@ -45,8 +29,25 @@ import java.util.TreeMap;
*/ */
public class ExcelWriteTest { 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.date());
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.date());
}
@Test @Test
public void writeNoFlushTest(){ public void writeNoFlushTest() {
List<?> row1 = CollUtil.newArrayList("aaaaa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765); List<?> row1 = CollUtil.newArrayList("aaaaa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676); List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676);
List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111); List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111);
@ -200,26 +201,13 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void mergeTest2() { public void mergeTest2() {
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
Map<String, Object> row2 = new LinkedHashMap<>(); ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(MAP_DATA_1, MAP_DATA_2);
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.date());
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
// 通过工具类创建writer // 通过工具类创建writer
ExcelWriter writer = ExcelUtil.getWriter("d:/test/writeMapTest.xlsx"); ExcelWriter writer = ExcelUtil.getWriter("d:/test/writeMapTest.xlsx");
// 合并单元格后的标题行使用默认标题样式 // 合并单元格后的标题行使用默认标题样式
writer.merge(row1.size() - 1, "一班成绩单"); writer.merge(MAP_DATA_1.size() - 1, "一班成绩单");
// 一次性写出内容使用默认样式强制输出标题 // 一次性写出内容使用默认样式强制输出标题
writer.write(rows, true); writer.write(rows, true);
@ -230,21 +218,8 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeMapTest() { public void writeMapTest() {
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
Map<String, Object> row2 = new LinkedHashMap<>(); ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(MAP_DATA_1, MAP_DATA_2);
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.date());
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
// 通过工具类创建writer // 通过工具类创建writer
ExcelWriter writer = ExcelUtil.getWriter("e:/excel/writeMapTest.xlsx"); ExcelWriter writer = ExcelUtil.getWriter("e:/excel/writeMapTest.xlsx");
@ -257,7 +232,7 @@ public class ExcelWriteTest {
writer.getStyleSet().setFont(font, true); writer.getStyleSet().setFont(font, true);
// 合并单元格后的标题行使用默认标题样式 // 合并单元格后的标题行使用默认标题样式
writer.merge(row1.size() - 1, "一班成绩单"); writer.merge(MAP_DATA_1.size() - 1, "一班成绩单");
// 一次性写出内容使用默认样式 // 一次性写出内容使用默认样式
writer.write(rows, true); writer.write(rows, true);
// 关闭writer释放内存 // 关闭writer释放内存
@ -267,18 +242,12 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeMapTest2() { public void writeMapTest2() {
Map<String, Object> row1 = MapUtil.newHashMap(true);
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
// 通过工具类创建writer // 通过工具类创建writer
ExcelWriter writer = ExcelUtil.getWriter("e:/writeMapTest2.xlsx"); ExcelWriter writer = ExcelUtil.getWriter("d:/writeMapTest2.xlsx");
// 一次性写出内容使用默认样式 // 一次性写出内容使用默认样式
writer.writeRow(row1, true); writer.writeRow(MAP_DATA_1, true);
// 关闭writer释放内存 // 关闭writer释放内存
writer.close(); writer.close();
} }
@ -286,12 +255,6 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeMapWithStyleTest() { public void writeMapWithStyleTest() {
Map<String, Object> row1 = MapUtil.newHashMap(true);
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
// 通过工具类创建writer // 通过工具类创建writer
String path = "f:/test/writeMapWithStyleTest.xlsx"; String path = "f:/test/writeMapWithStyleTest.xlsx";
@ -300,7 +263,7 @@ public class ExcelWriteTest {
writer.setStyleSet(null); writer.setStyleSet(null);
// 一次性写出内容使用默认样式 // 一次性写出内容使用默认样式
writer.writeRow(row1, true); writer.writeRow(MAP_DATA_1, true);
// 设置某个单元格样式 // 设置某个单元格样式
CellStyle orCreateRowStyle = writer.getOrCreateCellStyle(0, 1); CellStyle orCreateRowStyle = writer.getOrCreateCellStyle(0, 1);
@ -335,8 +298,8 @@ public class ExcelWriteTest {
writer.addHeaderAlias("name", "姓名"); writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年龄"); writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("score", "分数"); writer.addHeaderAlias("score", "分数");
writer.addHeaderAlias("isPass", "是否通过"); writer.addHeaderAlias("isPass", "是否合格");
writer.addHeaderAlias("examDate", "考试时间"); writer.addHeaderAlias("examDate", "考试日期");
// 合并单元格后的标题行使用默认标题样式 // 合并单元格后的标题行使用默认标题样式
writer.merge(4, "一班成绩单"); writer.merge(4, "一班成绩单");
// 一次性写出内容使用默认样式 // 一次性写出内容使用默认样式
@ -445,21 +408,21 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeBeanTest() { public void writeBeanTest() {
cn.hutool.poi.excel.TestBean bean1 = new cn.hutool.poi.excel.TestBean(); TestBean bean1 = new TestBean();
bean1.setName("张三"); bean1.setName("张三");
bean1.setAge(22); bean1.setAge(22);
bean1.setPass(true); bean1.setPass(true);
bean1.setScore(66.30); bean1.setScore(66.30);
bean1.setExamDate(DateUtil.date()); bean1.setExamDate(DateUtil.date());
cn.hutool.poi.excel.TestBean bean2 = new cn.hutool.poi.excel.TestBean(); TestBean bean2 = new TestBean();
bean2.setName("李四"); bean2.setName("李四");
bean2.setAge(28); bean2.setAge(28);
bean2.setPass(false); bean2.setPass(false);
bean2.setScore(38.50); bean2.setScore(38.50);
bean2.setExamDate(DateUtil.date()); bean2.setExamDate(DateUtil.date());
List<cn.hutool.poi.excel.TestBean> rows = CollUtil.newArrayList(bean1, bean2); List<TestBean> rows = CollUtil.newArrayList(bean1, bean2);
// 通过工具类创建writer // 通过工具类创建writer
String file = "e:/writeBeanTest.xlsx"; String file = "e:/writeBeanTest.xlsx";
FileUtil.del(file); FileUtil.del(file);
@ -481,17 +444,17 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeBeanTest2() { public void writeBeanTest2() {
cn.hutool.poi.excel.OrderExcel order1 = new cn.hutool.poi.excel.OrderExcel(); OrderExcel order1 = new OrderExcel();
order1.setId("1"); order1.setId("1");
order1.setNum("123"); order1.setNum("123");
order1.setBody("body1"); order1.setBody("body1");
cn.hutool.poi.excel.OrderExcel order2 = new cn.hutool.poi.excel.OrderExcel(); OrderExcel order2 = new OrderExcel();
order1.setId("2"); order1.setId("2");
order1.setNum("456"); order1.setNum("456");
order1.setBody("body2"); order1.setBody("body2");
List<cn.hutool.poi.excel.OrderExcel> rows = CollUtil.newArrayList(order1, order2); List<OrderExcel> rows = CollUtil.newArrayList(order1, order2);
// 通过工具类创建writer // 通过工具类创建writer
String file = "f:/test/writeBeanTest2.xlsx"; String file = "f:/test/writeBeanTest2.xlsx";
FileUtil.del(file); FileUtil.del(file);
@ -618,7 +581,7 @@ public class ExcelWriteTest {
Font headFont = writer.createFont(); Font headFont = writer.createFont();
headFont.setBold(true); headFont.setBold(true);
headFont.setFontHeightInPoints((short)50); headFont.setFontHeightInPoints((short) 50);
headFont.setFontName("Microsoft YaHei"); headFont.setFontName("Microsoft YaHei");
writer.getStyleSet().getHeadCellStyle().setFont(headFont); writer.getStyleSet().getHeadCellStyle().setFont(headFont);
@ -756,20 +719,20 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void mergeTest3(){ public void mergeTest3() {
// https://github.com/dromara/hutool/issues/1696 // https://github.com/dromara/hutool/issues/1696
List<Map<String,Object>> list = new ArrayList<>(); List<Map<String, Object>> list = new ArrayList<>();
Map<String,Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("xmnf","2021"); map.put("xmnf", "2021");
list.add(map); list.add(map);
Map<String,Object> map1 = new HashMap<>(); Map<String, Object> map1 = new HashMap<>();
map1.put("xmnf",new XSSFRichTextString("9999")); map1.put("xmnf", new XSSFRichTextString("9999"));
list.add(map1); list.add(map1);
Map<String,Object> map2 = new HashMap<>(); Map<String, Object> map2 = new HashMap<>();
map2.put("xmnf","2019"); map2.put("xmnf", "2019");
list.add(map2); list.add(map2);
//通过工具类创建writer //通过工具类创建writer
@ -786,7 +749,7 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void mergeForDateTest(){ public void mergeForDateTest() {
// https://github.com/dromara/hutool/issues/1911 // https://github.com/dromara/hutool/issues/1911
//通过工具类创建writer //通过工具类创建writer
@ -799,7 +762,7 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void changeHeaderStyleTest(){ public void changeHeaderStyleTest() {
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/headerStyle.xlsx"); final ExcelWriter writer = ExcelUtil.getWriter("d:/test/headerStyle.xlsx");
writer.writeHeadRow(ListUtil.of("姓名", "性别", "年龄")); writer.writeHeadRow(ListUtil.of("姓名", "性别", "年龄"));
final CellStyle headCellStyle = writer.getStyleSet().getHeadCellStyle(); final CellStyle headCellStyle = writer.getStyleSet().getHeadCellStyle();
@ -811,7 +774,7 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeFloatTest(){ public void writeFloatTest() {
//issue https://gitee.com/dromara/hutool/issues/I43U9G //issue https://gitee.com/dromara/hutool/issues/I43U9G
String path = "d:/test/floatTest.xlsx"; String path = "d:/test/floatTest.xlsx";
FileUtil.del(path); FileUtil.del(path);
@ -823,7 +786,7 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void issueI466ZZTest(){ public void issueI466ZZTest() {
// https://gitee.com/dromara/hutool/issues/I466ZZ // https://gitee.com/dromara/hutool/issues/I466ZZ
// 需要输出S_20000314_x5116_0004 // 需要输出S_20000314_x5116_0004
// 此处加入一个转义前缀_x005F // 此处加入一个转义前缀_x005F
@ -836,7 +799,7 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeLongTest(){ public void writeLongTest() {
//https://gitee.com/dromara/hutool/issues/I49R6U //https://gitee.com/dromara/hutool/issues/I49R6U
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/long.xlsx"); final ExcelWriter writer = ExcelUtil.getWriter("d:/test/long.xlsx");
writer.write(ListUtil.of(1427545395336093698L)); writer.write(ListUtil.of(1427545395336093698L));
@ -845,8 +808,8 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeHyperlinkTest(){ public void writeHyperlinkTest() {
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/hyperlink.xlsx"); final ExcelWriter writer = ExcelUtil.getWriter("d:/test/hyperlink.xlsx");
final Hyperlink hyperlink = writer.createHyperlink(HyperlinkType.URL, "https://hutool.cn"); final Hyperlink hyperlink = writer.createHyperlink(HyperlinkType.URL, "https://hutool.cn");
@ -856,12 +819,12 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void mergeNumberTest(){ public void mergeNumberTest() {
File tempFile=new File("d:/test/mergeNumber.xlsx"); File tempFile = new File("d:/test/mergeNumber.xlsx");
FileUtil.del(tempFile); FileUtil.del(tempFile);
BigExcelWriter writer= new BigExcelWriter(tempFile); BigExcelWriter writer = new BigExcelWriter(tempFile);
writer.merge(0,1,2,2,3.99,false); writer.merge(0, 1, 2, 2, 3.99, false);
writer.close(); writer.close();
} }
@ -880,7 +843,7 @@ public class ExcelWriteTest {
} }
@Test @Test
public void getDispositionTest(){ public void getDispositionTest() {
ExcelWriter writer = ExcelUtil.getWriter(true); ExcelWriter writer = ExcelUtil.getWriter(true);
final String disposition = writer.getDisposition("测试A12.xlsx", CharsetUtil.CHARSET_UTF_8); final String disposition = writer.getDisposition("测试A12.xlsx", CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals("attachment; filename=\"%E6%B5%8B%E8%AF%95A12.xlsx\"", disposition); Assert.assertEquals("attachment; filename=\"%E6%B5%8B%E8%AF%95A12.xlsx\"", disposition);
@ -888,32 +851,45 @@ public class ExcelWriteTest {
@Test @Test
@Ignore @Ignore
public void writeColTest(){ public void writeColTest() {
ExcelWriter writer = ExcelUtil.getWriter("G:\\test.xlsx"); ExcelWriter writer = ExcelUtil.getWriter("G:\\test.xlsx");
List<Integer> list= new ArrayList<>(); List<Integer> list = new ArrayList<>();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
list.add(i); list.add(i);
} }
writer.writeCol("header1",0,list,true); writer.writeCol("header1", 0, list, true);
writer.writeCol("header2",1,list,false); writer.writeCol("header2", 1, list, false);
writer.writeCol("header3",0,list,true); writer.writeCol("header3", 0, list, true);
writer.writeCol("header4",1,list,false); writer.writeCol("header4", 1, list, false);
Map<String,List<Integer>> map1 = new LinkedHashMap<>(); Map<String, List<Integer>> map1 = new LinkedHashMap<>();
map1.put("map1_header1",list); map1.put("map1_header1", list);
map1.put("map1_header2",list); map1.put("map1_header2", list);
map1.put("map1_header3",list); map1.put("map1_header3", list);
map1.put("map1_header4",list); map1.put("map1_header4", list);
Map<String,List<Integer>> map2 = new LinkedHashMap<>(); Map<String, List<Integer>> map2 = new LinkedHashMap<>();
map2.put("map2_header1",list); map2.put("map2_header1", list);
map2.put("map2_header2",list); map2.put("map2_header2", list);
map2.put("map2_header3",list); map2.put("map2_header3", list);
map2.put("map2_header4",list); map2.put("map2_header4", list);
writer.writeCol(map1,true); writer.writeCol(map1, true);
writer.writeCol(map2,false); writer.writeCol(map2, false);
writer.close(); writer.close();
} }
@Test
@Ignore
public void autoSizeColumnTest() {
Map<String, Object> map = new LinkedHashMap<>(MAP_DATA_1);
map.put("中文长度测试(符号)", "abc");
try (ExcelWriter writer = new ExcelWriter("d:/autoSizeColumnTest.xlsx")) {
writer.writeRow(map, true);
writer.autoSizeColumnAll(2f);
}
}
} }