创建word模板:
1.在对应的字段上用ctrl + F9
再邮件编辑域:
maven配置
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>2.0.2</version>
</dependency>
代码示例:
package com.hdlh.calculatenet.util;
import fr.opensagres.xdocreport.core.document.SyntaxKind;
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 fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import fr.opensagres.xdocreport.template.formatter.NullImageBehaviour;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;
import javax.servlet.ServletOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
/**
* @author zwj
* @date 2023年11月07日
* @description TODO
*/
public class DocUtil {
public static void main(String[] args) {
InputStream inputStream = null;
try {
FileOutputStream outputStream = new FileOutputStream("C:\\Users\\86180\\Desktop\\fabu\\导出工单报告-结果.docx");
//读取取resource目录下的模板
// inputStream = WordUtil.class.getClassLoader().getResourceAsStream("summary.docx");
inputStream = new FileInputStream("C:\\Users\\86180\\Desktop\\fabu\\导出工单报告-模板-开发.docx");
IXDocReport report = XDocReportRegistry
.getRegistry()
.loadReport(inputStream, TemplateEngineKind.Freemarker);
// 设置特殊字段
FieldsMetadata metadata = report.createFieldsMetadata();
metadata.addFieldAsTextStyling("content", SyntaxKind.Html);
metadata.addFieldAsImage("avatar", "user.avatar", NullImageBehaviour.RemoveImageTemplate);
report.setFieldsMetadata(metadata);
// 创建内容-text为模版中对应都变量名称
// String content = "<p>我在这里放了一段富文本</p>" +
// "<p>我准备测试富文本的处理</p>";
// content = HtmlUtils.htmlUnescape(content);
IContext context = report.createContext();
context.put("title", "网络数据共享审批表");
context.put("systemName", "供应链管理系统");
context.put("proposer", "申请人");
context.put("proposerDeptName", "申请人部门");
context.put("manufacturer", "华为科技有限公司");
context.put("manufacturerCurator", "华为余明");
context.put("sharedOutPoint", "共享输出点信息");
context.put("description", "共享数据约束信息");
context.put("opinions1", "同意");
context.put("sign1", "张三");
context.put("time1", "2023-11-06 10:23");
context.put("opinions2", "同意");
context.put("sign2", "李思");
context.put("time2", "2023-11-06 10:46");
context.put("opinions3", "同意");
context.put("sign3", "王武");
context.put("time3", "2023-11-06 11:23");
context.put("opinions4", "同意");
context.put("sign4", "赵柳");
context.put("time4", "2023-11-06 12:23");
// context.put("content", content);
// 生成文件
report.process(context, outputStream);
// response.setCharacterEncoding("utf-8");
// response.setContentType("application/msword");
// String fileName = "warning_task.docx";
// response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8")))) ;
report.process(context, outputStream);
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
另外有循环列表的设计:
左边编辑域插入三个参数即可:
@before-row[#list data as data]
${data.name}
@after-row[/#list]
后台java设置数据:
– opotions:是列表数据
context.put(“data”, opotions);