java导出word文档

这里写自定义目录标题

  • 参考链接
    • 优化示例

参考链接

https://www.toutiao.com/article/7050704086605382181/?app=news_article×tamp=1667958614&use_new_style=1&req_id=2022110909501401015016502014308E33&group_id=7050704086605382181&share_token=E5E1439A-5995-447C-8CF2-6451B1D5A90F&tt_from=weixin&utm_source=weixin&utm_medium=toutiao_ios&utm_campaign=client_share&wxshare_count=1&source=m_redirect

https://blog.csdn.net/lzh_java_demo/article/details/121122932

优化示例

  public void exportData(HttpServletResponse response) throws Exception {
        //获取Word模板,模板存放路径在项目的resources目录下
        ClassPathResource classPathResource = new ClassPathResource("template/附录二导出模板.docx");
        InputStream ins = new FileInputStream(classPathResource.getFile());
        //注册xdocreport实例并加载FreeMarker模板引擎
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
                TemplateEngineKind.Freemarker);
        //创建xdocreport上下文对象
        IContext context = report.createContext();
        //查询下部分列表数据
        IPage<PghAppendix2Lower> pageList = pageList(null, -1, -1);
        List<PghAppendix2Lower> lowerList = pageList.getRecords();
        context.put("lower", lowerList);
        //创建字段元数据
        FieldsMetadata fm = report.createFieldsMetadata();
        //Word模板中的表格数据对应的集合类型
        fm.load("lower", PghAppendix2Lower.class, true);
        //查询上部分数据
        List<PghAppendix2Upper> upperList = pghAppendix2UpperService.selectList(null, null);
        Map<String, List<PghAppendix2Upper>> groupMap = upperList.stream().collect(Collectors.groupingBy(u -> u.getType()));
        //类型:0.省调管理线路,1.地调调度线路,2.地调管理线路,3.说明,4.地调调度的变压器中性接地点
        String sdglxl = splicingStr(groupMap.get("0"));
        context.put("sdglxl", sdglxl);
        String ddddxl = splicingStr(groupMap.get("1"));
        context.put("ddddxl", ddddxl);
        String ddglxl = splicingStr(groupMap.get("2"));
        context.put("ddglxl", ddglxl);
        String sm = splicingStr(groupMap.get("3"));
        context.put("sm", sm);
        String ddddbyq = splicingStr(groupMap.get("4"));
        context.put("ddddbyq", ddddbyq);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/msword");
        String outFileName = "附录二.docx";
        response.setHeader("Content-Disposition", "attachment;filename=" + outFileName + ";filename*=utf-8''" + URLEncoder.encode(outFileName, "utf-8"));
        report.process(context, response.getOutputStream());
    }

#导出文字空格换行逻辑

   private String splicingStr(List<PghAppendix2Upper> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        String str = "\t";
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                str = str + "\t";
            }
            str = str + (i + 1) + ".  " + list.get(i).getDescribe();
            if (i != size - 1) {
                str = str + "\r\n";
            }
        }
        return str;
    }

你可能感兴趣的:(java,word,开发语言)