java使用word模板导出word文件(使用xdocreport的方式)

依赖的jar包

<dependency>
	    <groupId>fr.opensagres.xdocreport</groupId>
	    <artifactId>xdocreport</artifactId>
	    <version>2.0.2</version>
	</dependency>
  	<dependency>
	    <groupId>org.apache.velocity</groupId>
	    <artifactId>velocity</artifactId>
	    <version>1.7</version>
	</dependency>
  	<dependency>
	    <groupId>org.freemarker</groupId>
	    <artifactId>freemarker</artifactId>
	    <version>2.3.30</version>
	</dependency>
  	<dependency>
	    <groupId>oro</groupId>
	    <artifactId>oro</artifactId>
	    <version>2.0.8</version>
	</dependency>

编辑word模板

1、将光标放到需要添加方法的相应位置,插入文档部件中的域

 

添加动态图片

在word模板中任意插入一张图片,鼠标选中图片后点击插入书签

为书签添加变量名

开始编写代码

try{
    Resource resource = new ClassPathResource("com/test/base/template/template.docx");
	InputStream inputStream = resource.getInputStream();
	IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
	IContext context = report.createContext();
	//替换word模板中创建的域的变量
	context.put("name", "测试姓名");
	context.put("sex", "男");
	context.put("age", "28");
	//替换word模板中的动态图片
	IImageProvider zhaopian = new FileImageProvider(new File("E:\\图片\\IMG_20171005_102717.jpg"),true);
	FieldsMetadata metadata = report.createFieldsMetadata();
	metadata.addFieldAsImage("picture");
	report.setFieldsMetadata(metadata);
	context.put("picture", zhaopian);
	//导出word
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	report.process(context, bout);
	//创建文件输出流
	FileOutputStream out = new FileOutputStream("d:\\daochudeword.docx");
	out.write(bout.toByteArray());
	out.close();
	bout.close();
} catch (Exception e) {
	e.printStackTrace();
}

导出后的效果

 

 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Java可以使用第三方工具类库Hutool或者fr.opensagres.xdocreport来根据Word模板导出Word文档docx。其中,使用Hutool可以通过以下步骤实现: 1. 引入Hutool依赖: ``` <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.10</version> </dependency> ``` 2. 加载Word模板文件: ``` DocxWriter writer = new DocxWriter("template.docx"); ``` 3. 替换模板中的变量: ``` writer.replace("name", "张三"); ``` 4. 保存并关闭文件: ``` writer.flush(new FileOutputStream("output.docx")).close(); ``` 而使用fr.opensagres.xdocreport可以通过以下步骤实现: 1. 引入相关依赖: ``` <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.document.docx</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> ``` 2. 加载Word模板文件: ``` InputStream in = new FileInputStream(new File("template.docx"));IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker); ``` 3. 创建数据模型并填充数据: ``` IContext context = report.createContext(); context.put("name", "张三"); ``` 4. 生成Word文档并保存: ``` OutputStream out = new FileOutputStream(new File("output.docx")); report.process(context, out); out.close(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员老牛了laoliu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值