我想将JSP页中用 SQL select *From ...出来的数据倒出成excel文件,点击直接弹出对话框提示下载,用一下回贴的方法会报 bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
此行有错,到底是怎么回事?————————————————————————
——————————————————————
这是简单的JSP,你可以改造成SERVLET,使用方法,例如此文件名是getfile.jsp
与此文件的同目录下有一个test.doc的word目录,则用
http://localhost/getfile.jsp?file=test.doc
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%
String filename = "";
if (request.getParameter("file") != null) {
filename = request.getParameter("file");
}
response.setContentType("application/msword");
response.setHeader("Content-disposition","attachment; filename="+filename);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
System.out.println ( "³öÏÖIOException." + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
//return;
%>
-------------------------------------------
----------------------------------------
另外我又用了下面的方法也不行,报int status = khViewBean.getStatus(); 错啊,到底怎么搞?
请高手指教。---------------------------------------------------首先你的页面上要有一个按钮:save
这个按钮先做查询数据库,然后action转到保存excell的页面,即我给你的这些代码,由于我的是用这个页面是隐藏的,点击save时有跳出界面问你是打开还是保存,xxxxxxxxxxxxxx是你调用数据库的记录,
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.lang.*"%>
<%
try{
response.setContentType("Application/msexcel");
response.setHeader("Content-Disposition", "attachment;filename=trainlist.xls");
StringBuffer cont=new StringBuffer("");
%>
<HTML>
<HEAD>
<META http-equiv="Content-Style-Type" content="text/css">
</HEAD>
<BODY>
<br><br><br>
<%
int status = khViewBean.getStatus();
if (status == 0){
int rscount = khViewBean.getRSCount();
%>
<%
cont.append("<html><head><META content=\'text/html; charset=gb2312\' http-equiv=Content-Type></head><body>\r\n");
cont.append("<h1> SOPÎĵµ²éѯ½á¹û</h1>");
cont.append("<table width='100%' border='1'>\r\n");
cont.append("<tr>\r\n");
cont.append("<td height='19'>ÎļþºÅ </td>\r\n");
cont.append("<td height='19'>ÎļþÃû </td>\r\n");
cont.append("<td height='19'>²¿ÃÅ </td>\r\n");
cont.append("<td height='19'>ÉúЧʱ¼ä </td>\r\n");
cont.append("</tr>\r\n");
cont.append("<tr>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("</tr>\r\n");
cont.append("<tr>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
}
cont.append("</table>\r\n");
cont.append("</body></html>\r\n");
response.getWriter().println(cont.toString());
response.getWriter().close();
%>
<%
}
else if (status > 0) {
%>
<hr color="#6699CC">
<span class="text"><font color="blue" size="16"><center><P>ÎÞÊý¾Ý </P></center></font></span>
<%
}
else if (status < 0) {
%>
<hr color="#6699CC">
<span class="text"><font color="red" size="16"><center><P>·¢Éú´íÎó </P></center></font></span>
<%
}
%>
<hr color="#6699CC">
<%
}
catch (Exception e){
out.println(e.toString());
}
%>
</BODY>
</HTML>
此行有错,到底是怎么回事?————————————————————————
——————————————————————
这是简单的JSP,你可以改造成SERVLET,使用方法,例如此文件名是getfile.jsp
与此文件的同目录下有一个test.doc的word目录,则用
http://localhost/getfile.jsp?file=test.doc
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%
String filename = "";
if (request.getParameter("file") != null) {
filename = request.getParameter("file");
}
response.setContentType("application/msword");
response.setHeader("Content-disposition","attachment; filename="+filename);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(final IOException e) {
System.out.println ( "³öÏÖIOException." + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
//return;
%>
-------------------------------------------
----------------------------------------
另外我又用了下面的方法也不行,报int status = khViewBean.getStatus(); 错啊,到底怎么搞?
请高手指教。---------------------------------------------------首先你的页面上要有一个按钮:save
这个按钮先做查询数据库,然后action转到保存excell的页面,即我给你的这些代码,由于我的是用这个页面是隐藏的,点击save时有跳出界面问你是打开还是保存,xxxxxxxxxxxxxx是你调用数据库的记录,
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.lang.*"%>
<%
try{
response.setContentType("Application/msexcel");
response.setHeader("Content-Disposition", "attachment;filename=trainlist.xls");
StringBuffer cont=new StringBuffer("");
%>
<HTML>
<HEAD>
<META http-equiv="Content-Style-Type" content="text/css">
</HEAD>
<BODY>
<br><br><br>
<%
int status = khViewBean.getStatus();
if (status == 0){
int rscount = khViewBean.getRSCount();
%>
<%
cont.append("<html><head><META content=\'text/html; charset=gb2312\' http-equiv=Content-Type></head><body>\r\n");
cont.append("<h1> SOPÎĵµ²éѯ½á¹û</h1>");
cont.append("<table width='100%' border='1'>\r\n");
cont.append("<tr>\r\n");
cont.append("<td height='19'>ÎļþºÅ </td>\r\n");
cont.append("<td height='19'>ÎļþÃû </td>\r\n");
cont.append("<td height='19'>²¿ÃÅ </td>\r\n");
cont.append("<td height='19'>ÉúЧʱ¼ä </td>\r\n");
cont.append("</tr>\r\n");
cont.append("<tr>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("</tr>\r\n");
cont.append("<tr>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
cont.append("<td>"+xxxxxxxxxxxxxxxxxxxxxxxx +" </td>\r\n");
}
cont.append("</table>\r\n");
cont.append("</body></html>\r\n");
response.getWriter().println(cont.toString());
response.getWriter().close();
%>
<%
}
else if (status > 0) {
%>
<hr color="#6699CC">
<span class="text"><font color="blue" size="16"><center><P>ÎÞÊý¾Ý </P></center></font></span>
<%
}
else if (status < 0) {
%>
<hr color="#6699CC">
<span class="text"><font color="red" size="16"><center><P>·¢Éú´íÎó </P></center></font></span>
<%
}
%>
<hr color="#6699CC">
<%
}
catch (Exception e){
out.println(e.toString());
}
%>
</BODY>
</HTML>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货