当前位置:   article > 正文

java根据提供word模板导出word文档_javal根据浏览器模板导出word

javal根据浏览器模板导出word

涉及主要jar包为 freemarker-2.3.10.jar,servlet-api-2.4.jar。

(1)首先修改 word模板如下形式,把需要查询写入word的值用${}形式封装

(2)把 word改为ftl格式

首先word另存为选择xml

然后重命名直接将XML文件修改后缀名为ftl文件

(3)关键代码

  1. public void agentWordDownload(String agentCode, HttpServletRequest request,
  2. HttpServletResponse response) {
  3. //首先把word生成保存在某一地址
  4. boolean Wordsave = this.Wordsave(agentCode, request, response);
  5. //如果生成成功则可以下载
  6. if (Wordsave) {
  7. String url = AgentManageServiceSpringImpl.class.getResource("")
  8. .toString();
  9. // file:/Svn/20190219-myg/webapp/WEB-INF/classes/cn/com/cis/acic/sales/agreementManage/service/spring/
  10. //获取项目路径
  11. url = "/" + url.substring(6, url.indexOf("WEB-INF")) +
  12. "downloadFiles" + "/" + "agentInfo" + "/";
  13. //文件名字为 用户代码+.doc
  14. String fileName = agentCode + ".doc";
  15. try {
  16. request.setCharacterEncoding("utf-8");
  17. fileName = new String(fileName);
  18. //获取文件路径
  19. String filePath = url + fileName;
  20. filePath = (filePath == null) ? "" : filePath;
  21. response.setContentType("application/x-download");
  22. fileName = URLEncoder.encode(fileName, "UTF-8");
  23. response.addHeader("Content-Disposition",
  24. "attachment;filename=" + fileName);
  25. FileInputStream fis = null;
  26. OutputStream os = null;
  27. try {
  28. //把word信息写入response
  29. os = response.getOutputStream();
  30. fis = new FileInputStream(filePath);
  31. byte[] b = new byte[1024 * 10];
  32. int i = 0;
  33. while ((i = fis.read(b)) > 0) {
  34. os.write(b, 0, i);
  35. }
  36. os.flush();
  37. os.close();
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. } finally {
  41. if (fis != null) {
  42. try {
  43. fis.close();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. if (os != null) {
  49. try {
  50. os.close();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. } catch (UnsupportedEncodingException e) {
  57. e.printStackTrace();
  58. }
  59. } else {
  60. throw new BusinessException("代理人" + agentCode + "生成WORD文档出错", false);
  61. }
  62. }
  63. private boolean Wordsave(String agentCode, HttpServletRequest request,
  64. HttpServletResponse response) {
  65. boolean Wordsave = false;
  66. Map<String, Object> dataMap = new HashMap<String, Object>();
  67. try {
  68. //获取人员信息
  69. PrpSagent prpSagent = this.getPrpSagentByAgentCode(agentCode);
  70. if (prpSagent != null) {
  71. //将人员信息存入map集合
  72. dataMap.put("name",
  73. ((prpSagent.getAgentName() == null) ||
  74. "".equals(prpSagent.getAgentName())) ? ""
  75. : prpSagent.getAgentName());
  76. dataMap.put("sex",
  77. ((prpSagent.getSex() == null) ||
  78. "".equals(prpSagent.getSex())) ? "" : prpSagent.getSex());
  79. dataMap.put("school",
  80. ((prpSagent.getGraduateSchool() == null) ||
  81. "".equals(prpSagent.getGraduateSchool())) ? ""
  82. : prpSagent.getGraduateSchool());
  83. dataMap.put("educ",
  84. ((prpSagent.getEducation() == null) ||
  85. "".equals(prpSagent.getEducation())) ? ""
  86. : prpSagent.getEducation());
  87. dataMap.put("addressName",
  88. ((prpSagent.getAddressName() == null) ||
  89. "".equals(prpSagent.getAddressName())) ? ""
  90. : prpSagent.getAddressName());
  91. dataMap.put("idnumber",
  92. ((prpSagent.getIdentifyNumber() == null) ||
  93. "".equals(prpSagent.getIdentifyNumber())) ? ""
  94. : prpSagent.getIdentifyNumber());
  95. dataMap.put("phone",
  96. ((prpSagent.getPhoneNumber() == null) ||
  97. "".equals(prpSagent.getPhoneNumber())) ? ""
  98. : prpSagent.getPhoneNumber());
  99. Configuration configuration = new Configuration();
  100. configuration.setDefaultEncoding("utf-8");
  101. String url = AgentManageServiceSpringImpl.class.getResource("")
  102. .toString();
  103. // file:/F:/Svn/20190219-myg/webapp/WEB-INF/classes/cn/com/cis/acic/sales/agreementManage/service/spring/
  104. url = "/" + url.substring(6, url.indexOf("WEB-INF")) +
  105. "downloadFiles";
  106. //生成word存储某一路径
  107. System.out.println(url);
  108. // /F:/Svn/20190219-myg/webapp/downloadFiles
  109. configuration.setDirectoryForTemplateLoading(new File(url));
  110. File outFile = new File(url + "/" + "agentInfo" + "/" +
  111. agentCode + ".doc");
  112. //获取word模板
  113. Template template = configuration.getTemplate("个代信息登记表.ftl",
  114. "utf-8");
  115. //往模板中写入数据
  116. Writer out = new BufferedWriter(new OutputStreamWriter(
  117. new FileOutputStream(outFile), "utf-8"), 10240);
  118. template.process(dataMap, out);
  119. out.close();
  120. Wordsave = true;
  121. }
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. return Wordsave;
  126. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/617109
推荐阅读
相关标签
  

闽ICP备14008679号