poi TI版本为: 1.12.1
word表单模板,
核心生成表单代码
//定义数据集合
Map<String, Object> datas = new HashMap<>();
int deptNumber = 0;
for (String dept : Lists.newArrayList()) {
//标题解析
datas.put(String.format("title%s_%s"), String.format("%s%s年第%s次周例会汇报材料-%s","工作计划"));
//设置表单只有4个单元格,并设置单元格的大小
Tables.TableBuilder tableBuilder = Tables.ofA4ExtendWidth().percentWidth("100%", new int[]{7, 10, 10, 73});
//添加每行数据,.具体逻辑根据循环定义,内容省去
RowRenderData rowRenderData = Rows.of(fdOrder, cellGzdl, rwly, cellRenderData).center().rowHeight(2.54f).create();
//把内行添加到table 中去
tableBuilder.addRow(rowRenderData);
// 表单核心数据
datas.put(String.format("table%s_%s"), tableBuilder.create());
}
Configure config = Configure.builder().build();
try {
//根据模板,输出流提供前端使用
XWPFTemplate.compile(PropertiesUtils.getPath() + "template.docx", config).render(datas).writeAndClose(response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
//设置表格 内容,
private static Map<String, RowRenderData> getRowRenderData() {
Map<String, RowRenderData> title = new HashMap<>();
//标题完成情况 每个单元格内容
CellRenderData cell1 = getHearderCellRenderData("序号");
CellRenderData cell2 = getHearderCellRenderData("大类");
CellRenderData cell3 = getHearderCellRenderData("来源");
CellRenderData cell4 = getHearderCellRenderData("情况");
//表格一行数据
RowRenderData row0 = Rows.of(cell1, cell2, cell3, cell4).center().rowHeight(2.5f).create();
return title;
}
表格样式
/**
* 设置表格的样式
*/
private static CellRenderData getCellRenderData(String text) {
return Cells.of().addParagraph(getParagraphRenderData(text)).create();
}
/**
* 表头样式
*
* @param text
* @return
*/
private static CellRenderData getHearderCellRenderData(String text) {
return Cells.of().addParagraph(getParagraphRenderData(text, true)).create();
}
private static ParagraphRenderData getParagraphRenderData(String text) {
return getParagraphRenderData(text, false);
}
private static ParagraphRenderData getParagraphRenderData(String text, boolean title) {
//设置表格字体大小
Style style = Style.builder().buildFontSize(14).build();
//设置表格行距
ParagraphStyle build = ParagraphStyle
.builder()
.withSpacingRule(LineSpacingRule.EXACT)
.withSpacing(18).build();
if (title) {
style.setBold(true);
return Paragraphs.of(Texts.of(text).style(style).create()).paraStyle(build).center().create();
}
return Paragraphs.of(Texts.of(text).style(style).create()).paraStyle(build).left().create();
}