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();
IteratorString> 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"));
}
}
/**
* 檢查下載權(quán)限
* @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;ich.length;i++){
carnumber.append(s1).append(ch[i]).append("/w:t>/w:r>/w:p>/w:tc>");
}
carnumber.append("/w:tr>");
MapString,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;
}
}
}
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>!-- 輸出時緩沖區(qū)的大小 -->
/result>
/action>