java导出word(freemarker)

设计模板

        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<OutEvalPerson> personList = outEvalDeclareService.selectPersonList(declareId);
        context.put("personList", personList);
        //主要完成单位list
        List<String> idsList = outEvalDeclareService.selectDeptList(declareId);
        List<OutEvalDept> deptList = new ArrayList<>();
        if (idsList != null && idsList.size() > 0) {
            List<OutEvalDept> deptLists = outEvalDeclareService.selectDept(idsList);
            BatchOperateRequest batchOperateRequest = new BatchOperateRequest(idsList.toArray(new String[idsList.size()]));
            Resp<List<Dept>> res = remoteDeptService.getDeptList(batchOperateRequest);
            deptList = concat(deptLists, res);
        }
        context.put("deptList", deptList);
        //技术文件
        List<OutEvalFile> 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());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值