先贴代码:String fileName = model.get("planName").toString();
try {
FacesContext ctx = FacesContext.getCurrentInstance();
ctx.responseComplete();
String contentType = "application/msword";
HttpServletResponse response = (HttpServletResponse) ctx
.getExternalContext().getResponse();
StringBuffer strhtml = new StringBuffer();
response.setContentType(contentType);
strhtml.append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns:st1=\"urn:schemas-microsoft-com:office:smarttags\" xmlns=\"http://www.w3.org/TR/REC-html40\">");  
strhtml.append("<head><meta http-equiv=\"Content-Type\" content=\"application/msword; charset=utf-8\">");
strhtml.append("<meta name=\"ProgId\" content=\"Word.Document\">");
strhtml.append("<meta name=\"Generator\" content=\"Microsoft Word 12\">");
strhtml.append("<meta name=\"Originator\" content=\"Microsoft Word 12\">");
strhtml.append("<body>");
strhtml.append("j房间哦哦我皮肤饥饿我就份额位居份额为我 \n jofuwiofji额外好久哦哦文化噢日俄文化iu");
strhtml.append("</body></html>");
if (fileName != null && !"".equals(fileName)){
response.setHeader("Content-disposition",
"attachment;filename="
+ encodingFileName(fileName + ".doc"));
}else{
response.setHeader("Content-disposition",
"attachment;filename=" + fileName + ".doc");
}
PrintWriter os = response.getWriter();
try {
os.print(new String(strhtml.toString().getBytes("GBK"), "GBK"));
os.flush();
os.close();
} catch (Exception e) {
System.out.println("导出数据出错,错误信息为:" + e.toString());
if (os != null)
os.close();
}
ctx.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
但是这样设置了 输出的仍然是html 而不是word。
请教各位该如何解决。
谢谢。