栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java导出word(freemarker)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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());
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325648.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号