add writeHeader

This commit is contained in:
Looly
2024-08-29 20:24:30 +08:00
parent 54c858b8f1
commit b8991dbb76
6 changed files with 407 additions and 98 deletions

View File

@@ -662,52 +662,6 @@ public class ExcelWriteTest {
writer.close();
}
@Test
@Disabled
public void writeSecHeadRowTest() {
final List<?> row1 = ListUtil.of(1, "aa", "bb", "cc", "dd", "ee");
final List<?> row2 = ListUtil.of(2, "aa1", "bb1", "cc1", "dd1", "ee1");
final List<?> row3 = ListUtil.of(3, "aa2", "bb2", "cc2", "dd2", "ee2");
final List<?> row4 = ListUtil.of(4, "aa3", "bb3", "cc3", "dd3", "ee3");
final List<?> row5 = ListUtil.of(5, "aa4", "bb4", "cc4", "dd4", "ee4");
final List<List<?>> rows = ListUtil.of(row1, row2, row3, row4, row5);
// 通过工具类创建writer
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/writeSecHeadRowTest.xlsx");
final CellStyle cellStyle = writer.getWorkbook().createCellStyle();
cellStyle.setWrapText(false);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置标题内容字体
final Font font = writer.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 15);
font.setFontName("Arial");
//设置边框样式
StyleUtil.setBorder(cellStyle, BorderStyle.THICK, IndexedColors.RED);
cellStyle.setFont(font);
// 合并单元格后的标题行,使用设置好的样式
writer.merge(new CellRangeAddress(0, 1, 0, row1.size() - 1), "标题XXXXXXXX", cellStyle);
Console.log(writer.getCurrentRow());
//设置复杂表头
writer.merge(new CellRangeAddress(2, 3, 0, 0), "序号", true);
writer.merge(new CellRangeAddress(2, 2, 1, 2), "AABB", true);
writer.merge(new CellRangeAddress(2, 3, 3, 3), "CCCC", true);
writer.merge(new CellRangeAddress(2, 2, 4, 5), "DDEE", true);
writer.setCurrentRow(3);
final List<String> sechead = ListUtil.of("AA", "BB", "DD", "EE");
writer.writeSecHeadRow(sechead);
// 一次性写出内容,使用默认样式
writer.write(rows);
// 关闭writer释放内存
writer.close();
}
/**
* issue#1659@Github
* 测试使用BigWriter写出ExcelWriter修改失败
@@ -782,7 +736,7 @@ public class ExcelWriteTest {
@Disabled
public void changeHeaderStyleTest() {
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/headerStyle.xlsx");
writer.writeHeadRow(ListUtil.view("姓名", "性别", "年龄"));
writer.writeHeaderRow(ListUtil.view("姓名", "性别", "年龄"));
final CellStyle headCellStyle = ((DefaultStyleSet)writer.getStyleSet()).getHeadCellStyle();
headCellStyle.setFillForegroundColor(IndexedColors.YELLOW1.index);
headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

View File

@@ -0,0 +1,148 @@
package org.dromara.hutool.poi.excel.writer;
import org.apache.poi.ss.usermodel.*;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.poi.excel.ExcelUtil;
import org.dromara.hutool.poi.excel.RowGroup;
import org.dromara.hutool.poi.excel.style.StyleUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* https://blog.csdn.net/qq_45752401/article/details/121250993
*/
public class RowGroupTest {
@Test
@Disabled
void writeSingleRowGroupTest() {
final RowGroup rowGroup = RowGroup.of("分组表格测试");
final ExcelWriter writer = ExcelUtil.getWriter();
writer.writeHeader(rowGroup);
writer.flush(FileUtil.file("d:/test/poi/singleRowGroup.xlsx"), true);
writer.close();
}
@Test
@Disabled
void writeListRowGroupTest() {
final RowGroup rowGroup = RowGroup.of(null)
.addChild("标题1")
.addChild("标题2")
.addChild("标题3")
.addChild("标题4");
final ExcelWriter writer = ExcelUtil.getWriter();
writer.writeHeader(rowGroup);
writer.flush(FileUtil.file("d:/test/poi/listRowGroup.xlsx"), true);
writer.close();
}
@Test
@Disabled
void writeOneRowGroupTest() {
final RowGroup rowGroup = RowGroup.of("基本信息")
.addChild(RowGroup.of("名称2"))
.addChild(RowGroup.of("名称3"));
final ExcelWriter writer = ExcelUtil.getWriter();
writer.writeHeader(rowGroup);
writer.flush(FileUtil.file("d:/test/poi/oneRowGroup.xlsx"), true);
writer.close();
}
@Test
@Disabled
void writeOneRowGroupWithStyleTest() {
final ExcelWriter writer = ExcelUtil.getWriter();
final CellStyle cellStyle = writer.getWorkbook().createCellStyle();
cellStyle.setWrapText(false);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置标题内容字体
final Font font = writer.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 15);
font.setFontName("Arial");
//设置边框样式
StyleUtil.setBorder(cellStyle, BorderStyle.THICK, IndexedColors.RED);
cellStyle.setFont(font);
final RowGroup rowGroup = RowGroup.of("基本信息")
.setStyle(cellStyle)
.addChild(RowGroup.of("名称2"))
.addChild(RowGroup.of("名称3"));
writer.writeHeader(rowGroup);
writer.flush(FileUtil.file("d:/test/poi/oneRowGroupWithStyle.xlsx"), true);
writer.close();
}
@Test
@Disabled
void writeRowGroupTest() {
final RowGroup rowGroup = RowGroup.of("分组表格测试")
.addChild(RowGroup.of("序号"))
.addChild(
RowGroup.of("基本信息")
.addChild(RowGroup.of("名称1"))
.addChild(RowGroup.of("名称15")
.addChild(RowGroup.of("名称2"))
.addChild(RowGroup.of("名称3"))
)
.addChild(RowGroup.of("信息16")
.addChild(RowGroup.of("名称4"))
.addChild(RowGroup.of("名称5"))
.addChild(RowGroup.of("名称6"))
)
.addChild(RowGroup.of("信息7"))
)
.addChild(RowGroup.of("特殊信息")
.addChild(RowGroup.of("名称9"))
.addChild(RowGroup.of("名称17")
.addChild(RowGroup.of("名称10"))
.addChild(RowGroup.of("名称11"))
)
.addChild(RowGroup.of("名称18")
.addChild(RowGroup.of("名称12"))
.addChild(RowGroup.of("名称13"))
)
)
.addChild(RowGroup.of("名称14"));
final ExcelWriter writer = ExcelUtil.getWriter();
writer.writeHeader(rowGroup);
writer.flush(FileUtil.file("d:/test/poi/rowGroup.xlsx"), true);
writer.close();
}
@Test
@Disabled
void writeRowGroupTest2() {
final RowGroup rowGroup = RowGroup.of("分组表格测试")
.addChild(RowGroup.of("序号"))
.addChild(
RowGroup.of("基本信息")
.addChild(RowGroup.of("名称1"))
.addChild(RowGroup.of("名称15")
.addChild(RowGroup.of("名称2"))
.addChild(RowGroup.of("名称3")
.addChild(RowGroup.of("名称3-1"))
.addChild(RowGroup.of("名称3-2"))
)
)
.addChild(RowGroup.of("信息16")
.addChild(RowGroup.of("名称4"))
.addChild(RowGroup.of("名称5"))
.addChild(RowGroup.of("名称6"))
)
.addChild(RowGroup.of("信息7"))
);
final ExcelWriter writer = ExcelUtil.getWriter();
writer.writeHeader(rowGroup);
writer.flush(FileUtil.file("d:/test/poi/rowGroup2.xlsx"), true);
writer.close();
}
}