java使用 xdocreport,以 docx 模板方式进行 docx 文档的下载

简介: java使用 xdocreport,以 docx 模板方式进行 docx 文档的下载

gtihub 文档地址



项目中引入依赖

<!-- xdocreport -->
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.core</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
    <version>2.0.1</version>
</dependency>


控制层通过 HttpServletResponse 进行生成并调用

@RequestMapping(value = "/bdcdj/exportWord", method = RequestMethod.GET)
public void exportWord(@RequestParam(value = "countdate", required = false) String countdate, HttpServletResponse response) {
    InputStream inputStream = null;
    try {
        String msg = "";
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", false);
        resultMap.put("data", null);
        resultMap.put("msg", msg);
        if (StringUtils.isBlank(countdate)) {
            resultMap.put("msg", "参数为空,禁止获取数据");
            throw new RuntimeException("参数为空,禁止获取数据");
        }
        String[] split = countdate.split("-");
        if (split.length != 2) {
            resultMap.put("msg", "参数格式异常,禁止获取数据");
            throw new RuntimeException("参数格式异常,禁止获取数据");
        }
        // 解析参数,获取年份和月份
        String year = split[0];
        String month = split[1];
        Map<String, List<String>> countData = (Map<String, List<String>>) yangLingCountService.getCountData(year, month);
        month = month.replace("0", "");
        // 导出word
        String file = AppConfig.getEgovHome() + "/conf/server/template/杨凌不动产登记信息导出模板.docx";
        if (StringUtils.startsWith(file, "file")) {
            file = StringUtils.substring(file, file.indexOf("/") + 1);
        }
        inputStream = new FileInputStream(new File(file));
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
        IContext context = report.createContext();
        context.put("year", year);
        context.put("month", month);
        Iterator<Map.Entry<String, List<String>>> iterator = countData.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<String>> next = iterator.next();
            switch (next.getKey()) {
                case "table_1":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table1_" + i, next.getValue().get(i));
                    }
                    break;
                case "table_2":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table2_" + i, next.getValue().get(i));
                    }
                    break;
                case "table_5":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table5_" + i, next.getValue().get(i));
                    }
                    break;
                case "table_6":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table6_" + i, next.getValue().get(i));
                    }
                    break;
                case "table_7":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table7_" + i, next.getValue().get(i));
                    }
                    break;
                case "table_8":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table8_" + i, next.getValue().get(i));
                    }
                    break;
                case "table_9":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table9_" + i, next.getValue().get(i));
                    }
                    break;
                case "table_10":
                    for (int i = 0; i < next.getValue().size(); i++) {
                        context.put("table10_" + i, next.getValue().get(i));
                    }
                    break;
            }
        }
        context.put("test_content", "==测试内容==");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/msword");
        String fileName = "不动产登记信息统计表(月报)_" + System.currentTimeMillis() + ".docx";
        response.setHeader("Content-Disposition", "attachment;filename="
                .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
        report.process(context, response.getOutputStream());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != inputStream) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


模板内容

  • 通过 microsoft word 打开,WPS 慎用
  • 在你需要插入数据的地方,按 ctrl+f9


58454135d0a749b1af32a98e2df9ef25.png


  • 鼠标右键点击该区域,选择编辑域

71073109e1fd462a8411f590297eba52.png64de78ce4e364bd9878176487f44efc9.png


选择域名类型,填写域名的值。域名类型为:MergeField,域名格式为:${xxx}

445a78029252496598d39fa83820407b.png


点击确定保存

36d859681d114c1f8171203bbf5719a4.png


  • 保存模板文档


代码内容

  • 通过 content 对象(Map<String,Object> 形式)将内容设置进去

a8a7c465accb4821933742ed7618c002.png


导出后的效果

c0fc2b8e32ce406fa2e20532593aef1e.png

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
11小时前
|
数据采集 机器学习/深度学习 Java
数据猎手:使用Java和Apache HttpComponents库下载Facebook图像
本文介绍了如何使用Java和Apache HttpComponents库从Facebook获取图像数据。通过设置爬虫代理IP以避免限制,利用HttpClient发送请求,解析HTML找到图像链接,然后下载并保存图片。提供的Java代码示例展示了实现过程,包括创建代理配置、线程池,以及下载图片的逻辑。注意,实际应用需根据Facebook页面结构进行调整。
数据猎手:使用Java和Apache HttpComponents库下载Facebook图像
|
11小时前
|
缓存 前端开发 Java
15:Servlet 3.0文件上传与下载-Java Web
15:Servlet 3.0文件上传与下载-Java Web
28 5
|
11小时前
|
Java 关系型数据库 测试技术
Java代码一键生成数据库文档(案例详解)
Screw是一个自动化数据库文档生成工具,能根据数据库表结构快速生成简洁、多格式(HTML、Word、Markdown)的文档,支持MySQL、MariaDB等多数据库。它使用Freemarker模板,允许用户自定义样式。依赖包括HikariCP数据库连接池和对应JDBC驱动。通过在Java代码或Maven插件中配置,可方便生成文档。示例代码展示了如何在测试用例中使用Screw。文档效果依赖于数据库中的表和字段注释。
|
11小时前
|
JavaScript Java 测试技术
基于Java的珠宝购物网站系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的珠宝购物网站系统的设计与实现(源码+lw+部署文档+讲解等)
29 0
|
11小时前
|
JavaScript Java 测试技术
基于Java的电影评论系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的电影评论系统的设计与实现(源码+lw+部署文档+讲解等)
35 0
|
11小时前
|
JavaScript Java 测试技术
基于Java的在线日语培训平台的设计与实现(源码+lw+部署文档+讲解等)
基于Java的在线日语培训平台的设计与实现(源码+lw+部署文档+讲解等)
27 0
|
11小时前
|
JavaScript Java 测试技术
基于Java的同城蔬菜配送管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的同城蔬菜配送管理系统的设计与实现(源码+lw+部署文档+讲解等)
16 0
|
11小时前
|
JavaScript Java 测试技术
基于Java的心理预约咨询管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的心理预约咨询管理系统的设计与实现(源码+lw+部署文档+讲解等)
32 0
基于Java的心理预约咨询管理系统的设计与实现(源码+lw+部署文档+讲解等)
|
11小时前
|
JavaScript Java 测试技术
基于Java的企业员工岗前培训管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的企业员工岗前培训管理系统的设计与实现(源码+lw+部署文档+讲解等)
27 1
基于Java的企业员工岗前培训管理系统的设计与实现(源码+lw+部署文档+讲解等)
|
11小时前
|
JavaScript Java 测试技术
基于Java的电竞交互管理系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的电竞交互管理系统的设计与实现(源码+lw+部署文档+讲解等)
26 0
基于Java的电竞交互管理系统的设计与实现(源码+lw+部署文档+讲解等)