当前位置: 首页 > news >正文

网站建设网站维护网络免费推广平台

网站建设网站维护,网络免费推广平台,网站租用服务器,企业网站备案去哪里文章目录 1、Maven依赖2、.docx或.doc格式的word模板准备3、读word模板&#xff0c;批量替换代码域&#xff0c;生成文件&#xff0c;demo4、结果展示 1、Maven依赖 <dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagre…

文章目录

  • 1、Maven依赖
  • 2、.docx或.doc格式的word模板准备
  • 3、读word模板,批量替换代码域,生成文件,demo
  • 4、结果展示

1、Maven依赖

  <dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.core</artifactId><version>2.0.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document</artifactId><version>2.0.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template</artifactId><version>2.0.2</version></dependency><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>

2、.docx或.doc格式的word模板准备

  1. 创建.docx文件,编写内容

image.png

  1. 添加编辑域

按Ctrl+F9,创建编辑域
image.png
右击,选择编辑域
image.png
选择邮件合并,修改域代码要注意域代码的格式为:MERGEFIELD ${name}name为需要填充的内容。
image.png
        依次类推,填上所有的编辑域,调整文档格式等,就完成了word文档准备。
        之后需要代码读word模板转数据流,进行代码域的变量批量替换,重新写文件就完成的word文件生成工作。

3、读word模板,批量替换代码域,生成文件,demo

package com.dongzi.utils.word;import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;/*** 读word模板,生成数据*/
public class ReadWordTemplate {public static void main(String[] args) throws IOException, XDocReportException {// 从resources/template中获取word模板数据IXDocReport ixDocReport = readWord("info.docx");IContext context = ixDocReport.createContext();// set value// putTemplateValue_1(context);// putTemplateValue_2(context);putTemplateValue_3(context);// =============FileOutputStream out = new FileOutputStream("D:/temp/docx模板输出.docx");ixDocReport.process(context, out);out.flush();out.close();}public static IXDocReport readWord(String fileName) throws IOException, XDocReportException {// 读模板的方式// 方式1:通过URL加载
//        URL url = ClassLoader.getSystemClassLoader().getResource("template/" + fileName);
//        assert url != null;
//        InputStream in_1 = url.openStream();// 方式2:系统资源转数据流InputStream in_2 = ClassLoader.getSystemResourceAsStream("template/" + fileName);// 方式3:spring的类加载器,获取资源
//        ClassPathResource pathResource = new ClassPathResource("template/" + fileName);
//        InputStream in_3 = pathResource.getInputStream();return XDocReportRegistry.getRegistry().loadReport(in_2, TemplateEngineKind.Freemarker);}// 设置docx模板值public static void putTemplateValue_1(IContext context) {// docx文档模板读取,必须要预先再模板里面设置文本域,Ctrl+F9/*1. Ctrl+F9,打开编辑域2. 域选择“邮件合并”,域代码为:MERGEFIELD ${yourAddress}3. 页面展示的格式为:«${yourAddress}»,模板可替换*/context.put("yourName", "孙悟空");context.put("yourAge", "500");context.put("yourAddress", "花果山水帘洞");}public static void putTemplateValue_2(IContext context) {// 生成数据Map<String, Object> mapValues = new HashMap<>();mapValues.put("yourName", "齐天大圣");mapValues.put("yourAge", 36000);mapValues.put("yourAddress", "花果山水帘洞");//context.putMap(mapValues);}public static void putTemplateValue_3(IContext context) {// 生成数据Map<String, Object> mapValues = new HashMap<>();mapValues.put("yourName", "派大星");mapValues.put("yourAge", 36000);mapValues.put("yourAddress", "黄土高坡");for (int i = 1; i <= 10; i++) {mapValues.put("index_" + i, i);mapValues.put("value_" + i, i + 2);}//context.putMap(mapValues);}}

4、结果展示

image.png
至此,整个模板读取生成过程全部结束。

http://www.wangmingla.cn/news/34246.html

相关文章:

  • 深圳外贸建站网络推广公司成品短视频app下载有哪些软件
  • 关于网站建设的论文上海排名优化seo
  • 鸿运通网站建设怎么样网页设计与制作期末作品
  • 培训的网站建设企业seo推广外包
  • 嘉兴网站建设方案优化引擎搜索大全
  • 做招商加盟的网站网站建站系统
  • 有没有返利网站做京东的seo企业优化方案
  • 网站建设与管理案例教程seo优化专员招聘
  • 集团公司网站设计semester at sea
  • 网站报价模板seo推广学院
  • 哪个设计网站做兼职好深圳seo推广培训
  • 网站托管多少钱智慧营销系统平台
  • 如何设立网站网络营销期末总结
  • 网站的策划做推广口碑营销的产品有哪些
  • 方法数码做的网站怎么样seo 的作用和意义
  • 深圳做网站排名哪家好企业网站模板免费
  • ecshop 看网站首页大小国际新闻视频
  • 东莞商城网站建设哪家公司靠谱热点营销案例
  • oa系统网站建设做直销去哪里找客户
  • 做外贸网站的经验同城推广引流平台
  • 摄影网站开题报告广州seo推广培训
  • 模板网站怎么做宁波网站seo诊断工具
  • 甘肃建设厅职称查询官方网站网络营销公司招聘
  • 做树状图的网站中国万网域名注册免费
  • 国外产品网站中国足彩网竞彩推荐
  • 饿了吗网站有问题怎么办百度关键词搜索排名
  • wordpress模板tob乌鲁木齐seo
  • 仿顺丰优选网站源码站长seo查询
  • 网站内链的作用seo推广顾问
  • 如何 建公司网站搜百度盘