我做的項目原來是先在服務器上生成一個Excel文件,然后用jspsmartupload下載的,可是由于用jspsmartupload下載的excel文件由于編碼問題會有損壞,而且服務器的壓力也太大,所以改為在Action中生成excel文件,然后下載,方便多了。由于項目的原因,excel文件是實時生成的,對于jxl的使用,大家可以參考jxl相關的文章。
有什么問題可以和我聯系。
MSN:whw_dream(AT)hotmail.com
代碼如下:
test.jsp
<%@ taglib uri="/WEB-INF/struts-Html.tld" <html:html>
<html:button property="button" onclick="printAll()">
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");//設定輸出文件頭
新聞熱點
疑難解答