1、后台
public ActionForward onlyPrint(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
String name = "人事信息表.xls";
//String exePath = "D://Tomcat 5.0//webapps//oa//"+name+"//";
String exePath =this.getServlet().getServletContext().getRealPath("/")+"UploadFiles//"+name;
System.out.println("exePath="+exePath);
File fdown = new File(exePath);
FileInputStream fi = new FileInputStream(fdown);
OutputStream o=response.getOutputStream();
try {
int filelength = Integer.parseInt(String.valueOf(fdown.length()));
response.setContentType("application/msexcel;charset=gbk");
response.setHeader("Content-Disposition", "attachment;" + "filename="
+URLEncoder.encode(name, "UTF-8"));
response.setContentLength(filelength);
byte b[]=new byte[filelength];
int n = 0;
while((n=fi.read(b))!=-1) {
o.write(b,0,n);
}
} catch(Exception e){
e.printStackTrace();
} finally {
fi.close();
o.close();
}
2、<a href="javascript:systemPrint()">【输出】</a>
//输出
function systemPrint()
{
document.noticeForm.action="staffpost.do?method=onlyPrint";
document.noticeForm.submit();
}