crtl+F9编辑域
//获取Word模板,模板存放路径在项目的resources目录下
// InputStream ins = this.getClass().getResourceAsStream("成果评价申报表.docx");
InputStream ins = new FileInputStream("E:/programme/tams/tams-outcome-evaluation/outcome-evaluation-service/src/main/resources/成果评价申报表.docx");
//注册xdocreport实例并加载FreeMarker模板引擎
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
TemplateEngineKind.Freemarker);
//创建xdocreport上下文对象
IContext context = report.createContext();
OutevalDeclare outevalDeclare = outevalDeclareService.selectDeclare(declareId);
//创建要替换的文本变量
context.put("name", outevalDeclare.getName());
context.put("office", outevalDeclare.getOffice());
context.put("origin", outevalDeclare.getOrigin());
context.put("startDate", outevalDeclare.getStartDate());
context.put("endDate", outevalDeclare.getEndDate());
context.put("applyOffice", outevalDeclare.getApplyOffice());
context.put("person", outevalDeclare.getPerson());
context.put("phone", outevalDeclare.getPhone());
context.put("content", outevalDeclare.getContent());
context.put("innovation", outevalDeclare.getInnovation());
//填写说明
OutevalPeriod outevalPeriod = outevalPeriodService.selectPeriod(outevalDeclare.getPeriod());
context.put("instruction", outevalPeriod.getInstruction());
//主要研制人员list
List personList = outevalDeclareService.selectPersonList(declareId);
context.put("personList", personList);
//主要完成单位list
List idsList = outevalDeclareService.selectDeptList(declareId);
List deptList = new ArrayList<>();
if (idsList != null && idsList.size() > 0) {
List deptLists = outevalDeclareService.selectDept(idsList);
BatchOperateRequest batchOperateRequest = new BatchOperateRequest(idsList.toArray(new String[idsList.size()]));
Resp> res = remoteDeptService.getDeptList(batchOperateRequest);
deptList = concat(deptLists, res);
}
context.put("deptList", deptList);
//技术文件
List fileList = outevalDeclareService.selectFileList(declareId);
context.put("fileList", fileList);
//创建字段元数据
Fieldsmetadata fm = report.createFieldsmetadata();
//Word模板中的表格数据对应的集合类型
fm.load("personList", OutevalPerson.class, true);
fm.load("deptList", OutevalDept.class, true);
fm.load("fileList", OutevalFile.class, true);
report.setFieldsmetadata(fm);
//输出到本地目录
// FileOutputStream out = new FileOutputStream(new File("D://成果评价申请表.docx"));
// report.process(context, out);
// out.flush();
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
String fileName = "成果评价申请表.docx";
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
report.process(context, response.getOutputStream());



