标签:des style blog http io ar color os 使用
在页面上填写值,然后导出到word模板中,并把页面上的值带到模板中,也就是导出word文档,提前有word 的模板形式,
1.jsp 页面
<table class="formTable"> <TR> <TD class="label">会议地点</TD> <TD class="content"> <INPUT id="meetingSite" type="text" class="text" name="CommonLink/meetingSite"></TD> <TD class="label">会议人员</TD> <TD> <INPUT id="meetingPerson" type="text" class="text" name="CommonLink/meetingPerson"></TD> </TR> <TR> <TD class="label">会议内容</TD> <TD class="content"> <INPUT id="meetingContent" type="text" class="text" name="CommonLink/meetingContent"></TD> <TD class="label">会议时间</TD> <TD><INPUT id="meetingDate" type="text" class="text" name="CommonLink/meetingDate"></TD> </TR> <TR> <TD class="label">总经理</TD> <TD class="content"> <INPUT id="manager" type="text" class="text" name="CommonLink/manager"></TD> <TD class="label">采购部门</TD> <TD><INPUT id="purchaseDep" type="text" class="text" name="CommonLink/purchaseDep"></TD> </TR> </table> <div id="btns" class="form-btns"> <INPUT value="导出word文档" type="button" class="btn" onclick="exportWord();"> </div>
2.js异步的方法
function exportWord(){ var data = setData(); jQuery.post("http://localhost:8080/expWord/GKBX29_word.jsp",data,function(data1){ var url = data1; //回调函数,返回值是地址,data1 window.open(url); //打开 }); } function setData(){ var data = {}; data.author=‘zzz‘; data.meetingSite = document.getElementById("meetingSite").value; data.purchaseDep = document.getElementById("purchaseDep").value; data.meetingPerson = document.getElementById("meetingPerson").value; data.meetingContent = document.getElementById("meetingContent").value; data.meetingDate = document.getElementById("meetingDate").value; data.manager = document.getElementById("manager").value; return data; }
3.GKBX29_word.jsp其实是后台的处理方法,因为本次开发用EOS,所以在jsp用request接收值,并传递到模板
<%@page import="java.util.HashMap"%> <%@page import="java.util.Map"%> <%@page import="com.boco.eoms.word.client.word"%> <% request.setCharacterEncoding("UTF-8"); String author = request.getParameter("author"); String meetingSite = request.getParameter("meetingSite"); String purchaseDep = request.getParameter("purchaseDep"); String meetingPerson = request.getParameter("meetingPerson"); String meetingContent = request.getParameter("meetingContent"); String meetingDate = request.getParameter("meetingDate"); String manager = request.getParameter("manager"); word w = new word(); Map map = new HashMap(); map.put("author", author); map.put("meetingSite", meetingSite); map.put("purchaseDep", purchaseDep); map.put("meetingPerson", meetingPerson); map.put("meetingContent", meetingContent); map.put("meetingDate", meetingDate); map.put("manager", manager); String url = w.replacWordByMap(map, "model"); //模板名称,默认是.doc文件 response.getWriter().write(url); %>
4. 生成word 的方法
package com.boco.eoms; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; //***************************************************************// //** 需要使用变量的地方可以用如下替换 ${name} ,name为变量名,在map中key使用变量名,value使用要替换成的字符串,如 map.put("name","栾主峰") **// //***************************************************************// public class word { /** * 根据传入的模板编号将变量替换成实际值之后生成word文档 * 需要使用变量的地方word中可以用变量替换 ,格式: ${变量名} 如: ${name} * name为变量名,在map中key使用变量名,value使用要替换成的字符串 * 如 map.put("name","栾主峰") * @param maps 变量集合 * @param DocName 模板名称 * @return 重新生成的文档名称包含全路径,返回-1时,文件生成错误 */ public String createWordByMap(Map<String, String> maps, String DocName) { String path = this.getClass().getClassLoader().getResource("/") .getPath() + "attach/";//获取基础路径 String docModelPath = path + "word/model/"; //生成模板所在路径 String docPath = path + "word/create/" + getCurrentDateTime("yyyyMMdd") + "/"; //+"/"+getCurrentDateTime("yyyyMMddHHmmssSSS"); java.io.File filetemp = new java.io.File(docPath); if (!filetemp.exists()) filetemp.mkdirs(); String destFile = docPath + getCurrentDateTime("yyyyMMddHHmmssSSS") + ".doc"; HWPFDocument document = new word().replaceDoc(docModelPath + DocName + ".doc", maps); if (document != null) { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); try { document.write(ostream); // 输出word文件 OutputStream outs = new FileOutputStream(destFile); outs.write(ostream.toByteArray()); outs.close(); } catch (IOException e) { e.printStackTrace(); destFile = "-1"; } } else destFile = "-1"; return destFile; } /** * * @param destFile * @param fileCon */ public void exportDoc(String destFile, String fileCon) { try { //doc content ByteArrayInputStream bais = new ByteArrayInputStream(fileCon .getBytes()); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(destFile); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 读取word模板并替换变量 * @param srcPath * @param map * @return */ public HWPFDocument replaceDoc(String srcPath, Map<String, String> map) { try { // 读取word模板 FileInputStream fis = new FileInputStream(new File(srcPath)); HWPFDocument doc = new HWPFDocument(fis); // 读取word文本内容 Range bodyRange = doc.getRange(); // 替换文本内容 for (Map.Entry<String, String> entry : map.entrySet()) { bodyRange.replaceText("${" + entry.getKey() + "}", entry .getValue()); } return doc; } catch (Exception e) { e.printStackTrace(); return null; } } //得到当前的系统时间 /* 根据输入的格式(String _dtFormat)得到当前时间格式 */ public String getCurrentDateTime(String _dtFormat) { String currentdatetime = ""; try { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dtFormat = new SimpleDateFormat(_dtFormat); currentdatetime = dtFormat.format(date); } catch (Exception e) { System.out.println("时间格式不正确"); e.printStackTrace(); } return currentdatetime; } }
5.模板中的样式
标签:des style blog http io ar color os 使用
原文地址:http://www.cnblogs.com/estellez/p/4110015.html