Struts实时生成Excel文件下载
2008-01-05 18:45:05 来源:WEB开发网我做的项目原来是先在服务器上生成一个Excel文件,然后用jspsmartupload下载的,可是由于用jspsmartupload下载的excel文件由于编码问题会有损坏,而且服务器的压力也太大,所以改为在Action中生成excel文件,然后下载,方便多了。由于项目的原因,excel文件是实时生成的,对于jxl的使用,大家可以参考jxl相关的文章。
有什么问题可以和我联系。
MSN:whw_dream(AT)hotmail.com
代码如下:
test.jsp
<%@ taglib uri="/WEB-INF/struts-Html.tld" PRefix="html" %>
<html:html>
<html:button property="button" >
DownLoad
</html:button>
</html:html>
<script language='javascript'>
function printAll(){ location.href="<%=request.getContextPath()%><%=request.getContextPath()%>/download.do"; }
</script>
DownloadAction.Java
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.OutputStream;
import test.whw.upload.ExcelBean;
/**
* <p>Title:DownloadAction </p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c) 2004 jiahansoft</p>
* <p>Company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/
public class DownloadAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
try{
String fname = "test";//Excel文件名
OutputStream os = response.getOutputStream();//取得输出流
response.reset();//清空输出流
response.setHeader("Content-disposition", "attachment; filename=" + fname + ".xls");//设定输出文件头
更多精彩
赞助商链接