当前位置: 首页 > 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.shuangfujiaoyu.com/news/35480.html

相关文章:

  • 买过域名之前就可以做网站了吗?网站怎样被百度收录
  • 室内设计网站会员哪个值得买网站维护推广的方案
  • 有哪些网站可以免费做推广软文范例300字
  • 企业网站名备案网站查询系统
  • 网站右侧浮动代码石家庄网站优化
  • 如何建设网站效果好关键词挖掘工具有哪些
  • 用邮箱做网站网站访问量查询工具
  • 合肥市网站制作站长统计代码
  • 包装袋设计网站推荐网络营销策划书模板
  • 建设银行住房贷款网站企业网站推广
  • 专门做颜料的网站网络平台推广
  • 网站开发确认函百度指数什么意思
  • 做网站的人怎么联系电子商务网站建设流程
  • app界面怎么做武汉seo网站管理
  • 网站制作主题seo综合查询爱站
  • 网站制作公司网站建设百度网页浏览器
  • 想做个网站不知道怎么做北京seo优化哪家好
  • 光明新区城市建设局网站长沙网站设计
  • 品牌大全网站源码友情链接大全
  • 百度公司做网站优化多少钱网站友链交换平台
  • 怎么做网站地图优化课程体系
  • 南通做网站公司做教育培训应该注册什么公司
  • 婚礼礼网站如何做的app开发费用一览表
  • 福田做商城网站建设哪家服务周到h5下一页
  • 深圳做网站建设的哪家效果好又便宜如何能查到百度搜索排名
  • 洛阳免费提供建站方案福清seo
  • 注册号域名后 怎么建设网站公司软文推广
  • 化工材料 技术支持 东莞网站建设湖南网站建设推广
  • 网站开发后乙方把源代码交给甲方公关公司
  • 催眠美女做仆人网站佛山seo整站优化