漁人碼頭

          天行健,君子以自強(qiáng)不息。地勢(shì)坤,君子以厚德載物。
          posts - 12, comments - 16, trackbacks - 0, articles - 43
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Struts實(shí)時(shí)生成Excel文件下載

          Posted on 2007-01-16 17:51 Fisher 閱讀(361) 評(píng)論(0)  編輯  收藏 所屬分類: Struts

          我做的項(xiàng)目原來(lái)是先在服務(wù)器上生成一個(gè)excel文件,然后用jspsmartupload下載的,可是由于用jspsmartupload下載的excel文件由于編碼問(wèn)題會(huì)有損壞,而且服務(wù)器的壓力也太大,所以改為在Action中生成excel文件,然后下載,方便多了。由于項(xiàng)目的原因,excel文件是實(shí)時(shí)生成的,對(duì)于jxl的使用,大家可以參考jxl相關(guān)的文章。
          有什么問(wèn)題可以和我聯(lián)系。
          MSN:whw_dream(AT)hotmail.com
          代碼如下:
          test.jsp



          <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
          <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");//設(shè)定輸出文件頭
          ????? response.setContentType("application/msexcel");//定義輸出類型
          ????? ExcelBean eb = new ExcelBean();
          ????? eb.expordExcel(os);//調(diào)用生成excel文件bean
          ??? }catch(Exception e){
          ????? System.out.println(e);
          ??? }

          ??? return mapping.findForward("display");
          ? }
          }


          ExcelBean.java


          package test.whw.upload;
          import java.io.*;
          import jxl.*;
          import jxl.write.*;
          import jxl.format.*;
          import java.util.*;
          import java.awt.Color;

          public class ExcelBean {
          ? public ExcelBean(){}
          ? public String expordExcel(OutputStream os)throws Exception{
          ??? jxl.write.WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件
          ??? String tmptitle = "測(cè)試文件"; //標(biāo)題
          ??? jxl.write.WritableSheet wsheet = wbook.createSheet("第一頁(yè)", 0); //sheet名稱
          ??? //設(shè)置excel標(biāo)題
          ??? jxl.write.WritableFont wfont = new jxl.write.WritableFont(
          ??????? WritableFont.ARIAL, 16,
          ??????? WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE,
          ??????? jxl.format.Colour.BLACK);
          ??? jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
          ??????? wfont);
          ??? jxl.write.Label wlabel1;
          ??? wlabel1 = new jxl.write.Label(5, 0, tmptitle, wcfFC);
          ??? wsheet.addCell(wlabel1);
          ??? wfont = new jxl.write.WritableFont(
          ??????? WritableFont.ARIAL, 14,
          ??????? WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE,
          ??????? jxl.format.Colour.BLACK);
          ??? wcfFC = new jxl.write.WritableCellFormat(
          ??????? wfont);
          ??? jxl.write.Label wlabel;
          ??? wlabel = new jxl.write.Label(0, 0, "寫(xiě)入內(nèi)容");
          ??? wsheet.addCell(wlabel); //
          ??? wbook.write(); //寫(xiě)入文件
          ??? wbook.close();
          ??? os.close();
          ??? return "success";
          ? }
          }



          struts-config.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "<struts-config>
          ? <action-mappings>
          ??? <action type="test.whw.upload.DownloadAction" path="/download">
          ????? <forward name="display" path="/display.jsp" />
          ??? </action>
          ? </action-mappings>
          </struts-config>
          <!--display.jsp是成功的提示頁(yè)面-->




          Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=181276


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 遵化市| 开阳县| 河北区| 格尔木市| 永善县| 中山市| 化隆| 梅州市| 上思县| 贡山| 增城市| 介休市| 祁东县| 富源县| 泾源县| 长子县| 郸城县| 师宗县| 筠连县| 富源县| 平武县| 孝感市| 长岛县| 禄丰县| 贺兰县| 乐昌市| 区。| 庆云县| 正镶白旗| 吉木萨尔县| 镇宁| 新田县| 报价| 兰考县| 中超| 宿迁市| 汨罗市| 施甸县| 饶阳县| 万安县| 江门市|