1.导入依赖poi-tl(这里要用的几个方法在低版本的依赖包里是没有的,所以导包时注意版本)
最初导的1.6.0版本,操作的时候报错,显示有冲突,在项目里搜了好久,也没发现别的地方有引用,最后在maven的dependencies中发现是引入的版本中有几个冲突
降了一下poi-tl版本就没问题了。
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.9.1</version>
</dependency>
2.读取模板
1)通过模板在本地位置获取文件名
String resource = "C:\\模板.docx";
2)通过模板在OSS上位置获取文件名
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();// 创建OSSClient实例
conf.setSupportCname(false);// 私有云要关闭CNAME
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret,conf);
File newFile = new File("模板.docx");
try {
ossClient.getObject(new GetObjectRequest(bucketName, autotemplate), newFile);
} catch (Exception e) {
log.error("OSS连接异常或者该"+autotemplate+"路径下没有模板");
return;
}
String resource = newFile.getName();// 获取文件名
3.定义模板中需要的数据(按需要定义,可以是map,也可以是实体类)
Map<String, Object> data = new HashMap<>();
4.模板中已经有表格格式了,只需要按照表格格式往里加数据
//有多个表格时
Configure configDlyc = Configure.builder()
.bind("dlycList", new DetailTablePolicy())
.bind("djycList", new DetailTablePolicy())
.bind("jyjhList", new DetailTablePolicy())
.build();
在DetailTablePolicy类中处理数据
public class DetailTablePolicy extends DynamicTableRenderPolicy {
// 填充数据所在行数
int row = 1;
@Override
public void render(XWPFTable table, Object data) throws Exception {
if (null == data) return;
// 添加边框
CTTblBorders borders = table.getCTTbl().getTblPr().addNewTblBorders();
CTBorder hBorder = borders.addNewInsideH();
hBorder.setVal(STBorder.Enum.forString("single"));
hBorder.setSz(new BigInteger("1"));
hBorder.setColor("000000");
CTBorder vBorder = borders.addNewInsideV();
vBorder.setVal(STBorder.Enum.forString("single"));
vBorder.setSz(new BigInteger("1"));
vBorder.setColor("000000");
CTBorder lBorder = borders.addNewLeft();
lBorder.setVal(STBorder.Enum.forString("single"));
lBorder.setSz(new BigInteger("1"));
lBorder.setColor("000000");
CTBorder rBorder = borders.addNewRight();
rBorder.setVal(STBorder.Enum.forString("single"));
rBorder.setSz(new BigInteger("1"));
rBorder.setColor("000000");
CTBorder tBorder = borders.addNewTop();
tBorder.setVal(STBorder.Enum.forString("single"));
tBorder.setSz(new BigInteger("1"));
tBorder.setColor("000000");
CTBorder bBorder = borders.addNewBottom();
bBorder.setVal(STBorder.Enum.forString("single"));
bBorder.setSz(new BigInteger("1"));
bBorder.setColor("000000");
List<RowRenderData> goods = (List<RowRenderData>) data;
//获取表格的列数
List<CellRenderData> cellList = goods.get(0).getCells();
int maxCell = cellList.size();
if (null != goods) {
table.removeRow(this.row);
for (int i = 0; i < goods.size(); i++) {
XWPFTableRow insertNewTableRow = table.insertNewTableRow(this.row);
for (int j = 0; j < maxCell; j++) insertNewTableRow.createCell();
TableRenderPolicy.Helper.renderRow(table.getRow(this.row), goods.get(i));
List<XWPFTableCell> cells = insertNewTableRow.getTableCells();
//为数据指定文字样式
for (int j = 0; j < cells.size(); j++) {
List<XWPFParagraph> paragraphs = cells.get(j).getParagraphs();
for (int m=0; m<paragraphs.size();m++){
XWPFParagraph xwpfParagraph = paragraphs.get(m);
List<XWPFRun> runs = xwpfParagraph.getRuns();
for (int k=0;k<runs.size();k++){
XWPFRun xwpfRun = runs.get(k);
xwpfRun.setFontFamily("宋体");
xwpfRun.setFontSize(10);
}
}
}
}
}
}
}
5.输出文件
XWPFTemplate template = XWPFTemplate.compile(String 文件名, configDlyc).render(定义的模板数据);
// 输出到任何流
template.write(OutputStream stream)
// 便捷的输出到文件
template.writeToFile(String path)