赞
踩
涉及主要jar包为 freemarker-2.3.10.jar,servlet-api-2.4.jar。
(1)首先修改 word模板如下形式,把需要查询写入word的值用${}形式封装
(2)把 word改为ftl格式
首先word另存为选择xml
然后重命名直接将XML文件修改后缀名为ftl文件
(3)关键代码
- public void agentWordDownload(String agentCode, HttpServletRequest request,
- HttpServletResponse response) {
- //首先把word生成保存在某一地址
- boolean Wordsave = this.Wordsave(agentCode, request, response);
-
- //如果生成成功则可以下载
- if (Wordsave) {
- String url = AgentManageServiceSpringImpl.class.getResource("")
- .toString();
- // file:/Svn/20190219-myg/webapp/WEB-INF/classes/cn/com/cis/acic/sales/agreementManage/service/spring/
- //获取项目路径
- url = "/" + url.substring(6, url.indexOf("WEB-INF")) +
- "downloadFiles" + "/" + "agentInfo" + "/";
-
- //文件名字为 用户代码+.doc
- String fileName = agentCode + ".doc";
-
- try {
- request.setCharacterEncoding("utf-8");
- fileName = new String(fileName);
-
- //获取文件路径
- String filePath = url + fileName;
- filePath = (filePath == null) ? "" : filePath;
-
- response.setContentType("application/x-download");
- fileName = URLEncoder.encode(fileName, "UTF-8");
- response.addHeader("Content-Disposition",
- "attachment;filename=" + fileName);
-
- FileInputStream fis = null;
- OutputStream os = null;
-
- try {
- //把word信息写入response
- os = response.getOutputStream();
- fis = new FileInputStream(filePath);
- byte[] b = new byte[1024 * 10];
- int i = 0;
- while ((i = fis.read(b)) > 0) {
- os.write(b, 0, i);
- }
-
- os.flush();
- os.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- } else {
- throw new BusinessException("代理人" + agentCode + "生成WORD文档出错", false);
- }
- }
-
- private boolean Wordsave(String agentCode, HttpServletRequest request,
- HttpServletResponse response) {
- boolean Wordsave = false;
- Map<String, Object> dataMap = new HashMap<String, Object>();
-
- try {
- //获取人员信息
- PrpSagent prpSagent = this.getPrpSagentByAgentCode(agentCode);
-
- if (prpSagent != null) {
- //将人员信息存入map集合
- dataMap.put("name",
- ((prpSagent.getAgentName() == null) ||
- "".equals(prpSagent.getAgentName())) ? ""
- : prpSagent.getAgentName());
- dataMap.put("sex",
- ((prpSagent.getSex() == null) ||
- "".equals(prpSagent.getSex())) ? "" : prpSagent.getSex());
- dataMap.put("school",
- ((prpSagent.getGraduateSchool() == null) ||
- "".equals(prpSagent.getGraduateSchool())) ? ""
- : prpSagent.getGraduateSchool());
- dataMap.put("educ",
- ((prpSagent.getEducation() == null) ||
- "".equals(prpSagent.getEducation())) ? ""
- : prpSagent.getEducation());
- dataMap.put("addressName",
- ((prpSagent.getAddressName() == null) ||
- "".equals(prpSagent.getAddressName())) ? ""
- : prpSagent.getAddressName());
- dataMap.put("idnumber",
- ((prpSagent.getIdentifyNumber() == null) ||
- "".equals(prpSagent.getIdentifyNumber())) ? ""
- : prpSagent.getIdentifyNumber());
- dataMap.put("phone",
- ((prpSagent.getPhoneNumber() == null) ||
- "".equals(prpSagent.getPhoneNumber())) ? ""
- : prpSagent.getPhoneNumber());
-
- Configuration configuration = new Configuration();
- configuration.setDefaultEncoding("utf-8");
-
- String url = AgentManageServiceSpringImpl.class.getResource("")
- .toString();
- // file:/F:/Svn/20190219-myg/webapp/WEB-INF/classes/cn/com/cis/acic/sales/agreementManage/service/spring/
- url = "/" + url.substring(6, url.indexOf("WEB-INF")) +
- "downloadFiles";
- //生成word存储某一路径
- System.out.println(url);
- // /F:/Svn/20190219-myg/webapp/downloadFiles
- configuration.setDirectoryForTemplateLoading(new File(url));
-
- File outFile = new File(url + "/" + "agentInfo" + "/" +
- agentCode + ".doc");
-
- //获取word模板
- Template template = configuration.getTemplate("个代信息登记表.ftl",
- "utf-8");
-
- //往模板中写入数据
- Writer out = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(outFile), "utf-8"), 10240);
- template.process(dataMap, out);
- out.close();
- Wordsave = true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return Wordsave;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。