pom.xml
<dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport</artifactId> <version>2.0.2</version> </dependency>
新建docx文件,插入=>域=>邮件合并=>MEREFILEID ${sex} "sex"为参数变量
插入完后为:
public static void main(String[] args) throws Exception { only_name(); } //实现类 public static void only_name() throws Exception { //1.通过freemarker模板引擎加载文档,并缓存到registry中 InputStream in = new FileInputStream(BASE_PATH + "only_name.docx"); IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker); //2.设置填充字段、填充类以及是否为list。 FieldsMetadata fieldsMetadata = report.createFieldsMetadata(); report.setFieldsMetadata(fieldsMetadata); IContext context = report.createContext(); context.put("name", "踢足球"); context.put("sex", "男"); OutputStream out = new FileOutputStream( new File(BASE_PATH + "only_name-out.docx")); report.process(context, out); }
生成后的文件结果为:
标签:docx,name,Xdocreport,生成,only,context,report,sex From: https://www.cnblogs.com/mytzq/p/16805199.html