jsp按格式导出doc文件实例详解
原理:doc文件其实可以保存为xml文件,该xml文件用字符串表示了doc文件的表现形式,我们只需要用Java将那些要填的内容替换掉然后下载给客户就行了。
1.首先是按照你的文档填写好数据。
2.将文档另存为xml文件,然后编辑该xml文件,将填好的内容用某种格式替换,如:将名字张三替换成${name}
3.读取文件,将文件中的${name}替换成真正的名字。
4.下载。
接下来看代码:
首先是那个转换类
package com.my.util;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.Iterator;import java.util.Map;public class DowloadDocByTemplate { public InputStream dowload(String templatePath,Map map) throws IOException{ File file = new File(templatePath); StringBuffer sb = new StringBuffer(); BufferedReader br; br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8")); String s; while ((s = br.readLine()) != null) { sb.append(s); } br.close(); s = sb.toString(); Iterator<String> keys = map.keySet().iterator(); while(keys.hasNext()) { String key = (String) keys.next(); s=s.replace(key, (String) map.get(key)); } return new ByteArrayInputStream(s.getBytes("UTF-8")); }}
然后是action:
/** * 检查下载权限 * @return */ public String downLoadcheck(){ return SUCCESS; } public String getTargetFileName(){ return "zengguoqg.doc"; } public InputStream getInputStream(){ //ExportToWordUtil etw = new ExportToWordUtil(); StringBuffer carnumber = new StringBuffer("<w:tr w:rsidR='00000000'> <w:trPr> <w:trHeight w:hRule='exact' w:val='482'/> </w:trPr>"); String s1 = "<w:tc><w:tcPr><w:tcW w:w=/"236/" w:type=/"dxa/"/></w:tcPr><w:p w:rsidR=/"00000000/" w:rsidRDefault=/"009F38EE/">" + "<w:pPr><w:rPr><w:rFonts w:hint=/"eastAsia/"/></w:rPr></w:pPr><w:r>" + "<w:rPr><w:rFonts w:hint=/"eastAsia/"/></w:rPr><w:t>"; String number = "450981199202073518"; char[] ch = number.toCharArray(); for(int i =0;i<ch.length;i++){ carnumber.append(s1).append(ch[i]).append("</w:t></w:r></w:p></w:tc>"); } carnumber.append("</w:tr>"); Map<String,String> map = new HashMap(); map.put("${name}","曾强"); map.put("${sex}","男"); map.put("${writeDate}", "2014.1.2"); map.put("${cardNumber}", "1100106"); map.put("${personNumber}", carnumber.toString()); map.put("${birthday}", "2014.1.2"); map.put("${Education}", "本科"); map.put("${marride}", "未婚"); map.put("${comeFrom}", "广西"); map.put("${nation}", "汉族"); map.put("${ResidenceAddress}", "广西"); map.put("${PostalCode}", "543002"); map.put("${QQ}", "1104566175"); map.put("${job}", "软件工程师"); map.put("${politicalStatus}", "政治面貌"); map.put("${GraduateSchool}", "学院"); map.put("${professional}", "软件工程"); map.put("${beginWorkTime}", "2014.1.2"); map.put("${bankCarNumber}", "622709"); map.put("${emergencyContactPerson}", "曾强"); map.put("${relationship}", "同学"); map.put("${ummoveTelephone}", "0774133"); map.put("${mobilePhone}", "1350335"); map.put("${adreess}", "广西"); map.put("${mainExperience}", "无工作"); map.put("${favoriteAndGoodAt}", "喜欢打篮球"); String path = ServletActionContext.getServletContext().getRealPath("/fileTemplate/renyuandengjibiao2.xml"); System.out.println(path); //return etw.export(path, "test.xml", map, "out.doc"); try { return new DowloadDocByTemplate().dowload(path, map); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
最后是Strut配置下载:
<package name="dowload" extends="struts-default"> <action name="dowload" class="zhaopingAction" method="downLoadcheck"> <result name="success" type="stream"> <param name="contentType">application/ZIP</param><!--默认为 text/plain--> <param name="inputName">inputStream</param> <param name="contentDisposition">filename="${targetFileName}"</param> <param name="bufferSize">4096</param><!-- 输出时缓冲区的大小 --> </result> </action>
完了,就这些了。xml文件我放在WebRoot/fileTemplate/renyuandengjibiao2.xml。
如有疑问,请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
新闻热点
疑难解答