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
- 鼠标右键点击该区域,选择编辑域
选择域名类型,填写域名的值。域名类型为:MergeField,域名格式为:${xxx}
点击确定保存
- 保存模板文档
代码内容
- 通过 content 对象(Map<String,Object> 形式)将内容设置进去
导出后的效果