官网链接
Poi-tl Documentation
引入依赖
maven
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.2</version>
</dependency>
Gradle
implementation 'com.deepoove:poi-tl:1.12.2'
基础案例一
package com.deepoove.poi.examples;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.CellStyle;
import com.deepoove.poi.data.style.ParagraphStyle;
import com.deepoove.poi.data.style.RowStyle;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.deepoove.poi.xwpf.NumFormat;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @program: base-utils.
* @description: TODO.
* @author: 111111.
* @created: 2024/04/09 08:34.
*/
public class WordTest02 {
public static void main(String[] args) throws IOException {
NiceXWPFDocument document = new NiceXWPFDocument();
Documents.DocumentBuilder builder = Documents.of();
BigInteger numId = document.addNewMultiLevelNumberingId(NumberingFormat.DECIMAL, new NumberingFormat(NumFormat.DECIMAL, "%1.%2."));
// 1.
ParagraphRenderData paraOfNumbering0 = Paragraphs.of().addText(
Texts.of("I consider myself the luckiest man").create())
.paraStyle(ParagraphStyle.builder().withNumId(numId.longValue()).withLvl(0).build()).create();
// 1. 1.
ParagraphRenderData paraOfNumbering00 = Paragraphs.of().addText("I consider myself the luckiest man")
.paraStyle(ParagraphStyle.builder().withNumId(numId.longValue()).withLvl(1).build()).create();
// 1. 1.
ParagraphRenderData paraOfNumbering01 = Paragraphs.of().addText("I consider myself the luckiest man")
.paraStyle(ParagraphStyle.builder().withNumId(numId.longValue()).withLvl(1).build()).create();
// 2.
ParagraphRenderData paraOfNumbering1 = Paragraphs.of().addText("I consider myself the luckiest man")
.paraStyle(ParagraphStyle.builder().withNumId(numId.longValue()).withLvl(0).build()).create();
builder.addParagraph(paraOfNumbering0)
.addParagraph(Paragraphs.of()
.addText("测试哇").create())
.addParagraph(paraOfNumbering00).addParagraph(paraOfNumbering01).addParagraph(paraOfNumbering1);
builder.addParagraph(Paragraphs.of("标题2").create());
RowRenderData row0 = Rows.of("列0", "列1", "列2").center().create();
RowRenderData row1 = Rows.create("没有数据", null, "郑州市航空港区金陵大道浮清河路口");
RowStyle rowStyle = new RowStyle();
CellStyle cellStyle = new CellStyle();
cellStyle.setDefaultParagraphStyle(ParagraphStyle.builder()
.withAlign(ParagraphAlignment.CENTER)
.withIndentFirstLineChars(0)
.build());
rowStyle.setDefaultCellStyle(cellStyle);
row1.setRowStyle(rowStyle);
builder.addTable(Tables.create(row0, row1)).create();
TableRenderData renderData = Tables.of(row0, row1).center().create();
builder.addParagraph(new ParagraphRenderData());
builder.addTable(renderData);
DocumentRenderData documentRenderData = builder.create();
XWPFTemplate template = XWPFTemplate.create(document,documentRenderData);
template.writeAndClose(Files.newOutputStream(Paths.get("c:/tmp/builder-02.docx")));
}
}
问题一 列宽自适应
- 上述生成的word文档中表格列宽无法使用自适应 对 第一个表格进行修改 使用 ofAutoWidth();
Tables.TableBuilder tableBuilder = Tables.ofAutoWidth();
tableBuilder.addRow(row0);
tableBuilder.addRow(row1);
builder.addTable(tableBuilder.create()).create();
/**
* 会发现如下问题 虽然确实是自适应了,但是宽度变得不是我们想要的了 这个宽度在哪里设置的呢
*/
经查阅源码 setWidth 方法中 TableWidthType.PCT 是多列均分表格宽度
而自适应 TableWidthType.AUTO 中未做宽度设定 那宽度从哪里来的呢?
继续查阅 点 table.setWidth(width); 方法进去,一直点到下面第三段代码中会发现,auto初始化为0 ctWidth.setW(BigInteger.ZERO)
public static TableBuilder of(RowRenderData... row) {
TableBuilder inst = ofPercentWidth("100%");
if (null != row) {
Arrays.stream(row).forEach(inst::addRow);
}
return inst;
}
...
public static void setWidth(XWPFTable table, String width, int[] colWidths) {
ensureTblW(table);
table.setWidth(width);
if (null == colWidths) {
int columnSize = TableTools.obtainColumnSize(table);
if (table.getWidthType() == TableWidthType.DXA) {
colWidths = UnitUtils.average(Integer.valueOf(width), columnSize);
} else if (table.getWidthType() == TableWidthType.PCT) {
int sum = 0;
colWidths = new int[columnSize];
for (int i = 0; i < columnSize - 1; i++) {
colWidths[i] = 100 / columnSize;
sum += colWidths[i];
}
colWidths[columnSize - 1] = 100 - sum;
} else if (table.getWidthType() == TableWidthType.AUTO) {
}
}
}
...
protected static void setWidthValue(String widthValue, CTTblWidth ctWidth) {
if (!widthValue.matches(REGEX_WIDTH_VALUE)) {
throw new RuntimeException("Table width value \"" + widthValue + "\" "
+ "must match regular expression \"" + REGEX_WIDTH_VALUE + "\".");
}
if (widthValue.matches("auto")) {
ctWidth.setType(STTblWidth.AUTO);
ctWidth.setW(BigInteger.ZERO);
} else if (widthValue.matches(REGEX_PERCENTAGE)) {
setWidthPercentage(ctWidth, widthValue);
} else {
// Must be an integer
ctWidth.setW(new BigInteger(widthValue));
ctWidth.setType(STTblWidth.DXA);
}
}
问题一解决方案
// 在 setWIdth方法中 添加如下代码
else if (table.getWidthType() == TableWidthType.AUTO) {
table.setWidth("100%");
}
在运行 会发现完全满足要求
/**
* 但是写死会有点不优雅 所以继续做修改 在 Tables.java 中增加如下方法
*/
// 这俩是 builder内的方法
public TableBuilder autoWidth(String autoWidth) {
TableStyle style = getTableStyle();
style.setWidth("auto,"+autoWidth);
return this;
}
public TableBuilder autoWidth(Integer autoWidth) {
return this.autoWidth(autoWidth+"%");
}
// 这个是Tables 的方法
public static TableRenderData createAutoWidth(RowRenderData... row) {
return of(row).autoWidth().create();
}
/**
* TableTools 中的 setWidth 方法 修改这个代码块
*/
public static void setWidth(XWPFTable table, String width, int[] colWidths) {
ensureTblW(table);
//table.setWidth(width); // TODO 这个设置会对宽度进行设定 ctWidth.setW(BigInteger.ZERO); 后续自己覆盖这个值即可
// TODO 由于需要动态的传入值 在此对width 进行修改 可正常传入 xxx% 但auto模式的数据是由逗号隔开的 eg. auto,xxx%
String percentage = "100%";
if(width.startsWith("auto,")) {
percentage = width.split(",")[1];
width = "auto";
}
table.setWidth(width);
if (null == colWidths) {
int columnSize = TableTools.obtainColumnSize(table);
if (table.getWidthType() == TableWidthType.DXA) {
colWidths = UnitUtils.average(Integer.valueOf(width), columnSize);
} else if (table.getWidthType() == TableWidthType.PCT) {
int sum = 0;
colWidths = new int[columnSize];
for (int i = 0; i < columnSize - 1; i++) {
colWidths[i] = 100 / columnSize;
sum += colWidths[i];
}
colWidths[columnSize - 1] = 100 - sum;
} else if (table.getWidthType() == TableWidthType.AUTO) {
table.setWidth(StringUtils.isNotBlank(width) ? width.equals("auto") ? "100%" : percentage : "100%");
}
}
// other codes....
}
调整案例代码
builder.addTable(Tables.createAutoWidth(row0, row1)).create();
TableRenderData renderData = Tables.of(row0, row1).center().create();
结果如下 同样满足自适应