Java利用poi-tl按模板动态生成Word

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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值