1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java根据提供word模板导出word文档

java根据提供word模板导出word文档

时间:2022-10-06 02:23:40

相关推荐

java根据提供word模板导出word文档

涉及主要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/0219-myg/webapp/WEB-INF/classes/cn/com/cis/acic/sales/agreementManage/service/spring///获取项目路径url = "/" + url.substring(6, url.indexOf("WEB-INF")) +"downloadFiles" + "/" + "agentInfo" + "/";//文件名字为 用户代码+.docString 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信息写入responseos = 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/0219-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/0219-myg/webapp/downloadFilesconfiguration.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;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。