隨筆 - 8  文章 - 24  trackbacks - 0
          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(4)

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          1.下載jxl.rar包

          項(xiàng)目地址:http://www.andykhan.com/jexcelapi/
          下載地址:http://www.andykhan.com/jexcelapi/download.html

          目前版本的信息
        1. Reads data from Excel 95, 97, 2000 workbooks
        2. Reads and writes formulas (Excel 97 and later only)
        3. Generates spreadsheets in Excel 2000 format
        4. Supports font, number and date formatting
        5. Supports shading and colouring of cells
        6. Modifies existing worksheets
        7. Supports image creation
        8. Preserves macros on copy
        9. Customizable logging


          2.把包放到WEB-INF的lib目錄下在開(kāi)發(fā)環(huán)境中引入這個(gè)包

          3.開(kāi)始寫(xiě)代碼了,這里以一個(gè)Struts1.2的ActionMethod為例,其實(shí)只要能取了request和response對(duì)象,操作都是一樣的的。

           1    /**
           2     * 生成信息的XLS
           3
               * alex 2007-7-3 下午05:01:56
           4     */

           5    public ActionForward makeRichVoteRZ(ActionMapping mapping, ActionForm form,
           6
                      HttpServletRequest request, HttpServletResponse response)
           7            throws Exception 
          {
           8
                  
           9        //讀出數(shù)據(jù)

          10        String richvote_id = Common.getValue("richvote_id", request);
          11        String sql = "select user_name,user_sex,user_address,card_id,postalcode,mobile,tel_day,email from tbl_member where member_id in (select user_id from tbl_vote_detail where vote_id in(select vote_id from tbl_vote where vote_board = '"+richvote_id+"'))"
          ;
          12        RowSet rs =
           table.select(sql);
          13
                  
          14        //生成xls

          15        try{
          16
                      
          17            response.setContentType("application/vnd.ms-excel"
          );
          18            response.addHeader("Content-Disposition","attachment;   filename=\""   +  Common.getFileName()+".xls"  +   "\""
          );    
          19            OutputStream os =
           response.getOutputStream();
          20            WritableWorkbook wwb =
           Workbook.createWorkbook(os);
          21
                      
          22
                      
          23            int ncout =
           rs.length();
          24            int maxnum = 50000//一次最多寫(xiě)入量

          25            int times = (ncout+maxnum-1)/maxnum;
          26
                      
          27            //大循環(huán)

          28            for(int t=0; t<times; t++){
          29
                          
          30                //新建一張表

          31                WritableSheet wsheet = wwb.createSheet("members_"+(t+1),t);
          32                //設(shè)置表頭

          33                Label label = new Label(0,0,"");
          34
                          wsheet.addCell(label);
          35                label = new Label(0,0,"會(huì)員姓名"
          );
          36
                          wsheet.addCell(label);
          37                label = new Label(1,0,"卡號(hào)"
          );
          38
                          wsheet.addCell(label);
          39                label = new Label(2,0,"聯(lián)系地址"
          );
          40
                          wsheet.addCell(label);
          41                label = new Label(3,0,"郵編"
          );
          42
                          wsheet.addCell(label);
          43                label = new Label(4,0,"聯(lián)系電話(huà)"
          );
          44
                          wsheet.addCell(label);
          45                label = new Label(5,0,"手機(jī)"
          );
          46
                          wsheet.addCell(label);
          47                label = new Label(6,0,"Email"
          );
          48
                          wsheet.addCell(label);
          49                label = new Label(7,0,"性別"
          );
          50
                          wsheet.addCell(label);
          51
                          
          52
                          
          53                //讀出數(shù)據(jù)

          54                int base = (t*maxnum);
          55                for(int i = 0; i < rs.length(); i++)
          {
          56                    Row rw = rs.get(i+
          base);
          57                    //System.out.println((i+1));

          58                    label = new Label(0,(i+1),(String)rw.get("user_name")    );
          59
                              wsheet.addCell(label);
          60                    label = new Label(1,(i+1),(String)rw.get("card_id"
          ));
          61
                              wsheet.addCell(label);
          62                    label = new Label(2,(i+1),(String)rw.get("user_address"
          ));
          63
                              wsheet.addCell(label);
          64                    label = new Label(3,(i+1),(String)rw.get("postalcode"
          ));
          65
                              wsheet.addCell(label);
          66                    label = new Label(4,(i+1),(String)rw.get("tel_day"
          ));
          67
                              wsheet.addCell(label);
          68                    label = new Label(5,(i+1),(String)rw.get("mobile"
          ));
          69
                              wsheet.addCell(label);
          70                    label = new Label(6,(i+1),(String)rw.get("email"
          ));
          71
                              wsheet.addCell(label);
          72                    label = new Label(7,(i+1),(String)rw.get("user_sex"
          ));
          73
                              wsheet.addCell(label);
          74                }
              
          75
                          
          76            }
          //結(jié)束大循環(huán)
          77            
          78
                      wwb.write();
          79
                      wwb.close();
          80
                      os.close();
          81
                      response.flushBuffer();
          82
                      
          83        }
          catch(Exception e){
          84            System.out.println("生成信息表(Excel格式)時(shí)出錯(cuò):"
          );
          85
                      e.printStackTrace();
          86        }

          87        
          88        return null
          ;
          89    }


          代碼簡(jiǎn)單說(shuō)明:
          1.設(shè)定好response的相關(guān)屬性:
          response.setContentType("application/vnd.ms-excel");
          response.addHeader("Content-Disposition","attachment;   filename=\""   +  Common.getFileName()+".xls"  +   "\""); 
          2.取到response的OutputStream實(shí)例,并用這個(gè)實(shí)例化一個(gè)WritableWorkbook對(duì)象
          OutputStream os = response.getOutputStream();
          WritableWorkbook wwb = Workbook.createWorkbook(os);
          3.新建一個(gè)表
          WritableSheet wsheet = wwb.createSheet("members_"+(t+1),t);
          4.往表里加行頭
          Label label = new Label(0,0,"");
              wsheet.addCell(label);
              label = new Label(0,0,"會(huì)員姓名");
              wsheet.addCell(label);
              label = new Label(1,0,"卡號(hào)");
              wsheet.addCell(label);
              label = new Label(2,0,"聯(lián)系地址");
              wsheet.addCell(label);
              label = new Label(3,0,"郵編");
              wsheet.addCell(label);
              label = new Label(4,0,"聯(lián)系電話(huà)");
              wsheet.addCell(label);
              label = new Label(5,0,"手機(jī)");
              wsheet.addCell(label);
              label = new Label(6,0,"Email");
              wsheet.addCell(label);
              label = new Label(7,0,"性別");
              wsheet.addCell(label);
          5.往表里加數(shù)據(jù)行
          for(int i = 0; i < rs.length(); i++){
               Row rw = rs.get(i+base);
               //System.out.println((i+1));
               label = new Label(0,(i+1),(String)rw.get("user_name") );
               wsheet.addCell(label);
               label = new Label(1,(i+1),(String)rw.get("card_id"));
               wsheet.addCell(label);
               label = new Label(2,(i+1),(String)rw.get("user_address"));
               wsheet.addCell(label);
               label = new Label(3,(i+1),(String)rw.get("postalcode"));
               wsheet.addCell(label);
               label = new Label(4,(i+1),(String)rw.get("tel_day"));
               wsheet.addCell(label);
               label = new Label(5,(i+1),(String)rw.get("mobile"));
               wsheet.addCell(label);
               label = new Label(6,(i+1),(String)rw.get("email"));
               wsheet.addCell(label);
               label = new Label(7,(i+1),(String)rw.get("user_sex"));
               wsheet.addCell(label);
              } 
          6.把生成的excel數(shù)據(jù)輸出到response的OutputStream
          wwb.write();
             wwb.close();
             os.close();
             response.flushBuffer();
          7.完成

          有什么疑問(wèn)可以留言,我會(huì)盡量幫助。

        10. posted on 2007-09-20 14:47 Vincent.Yu 閱讀(12523) 評(píng)論(10)  編輯  收藏

          FeedBack:
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2007-09-27 19:30 千里冰封
          呵呵,POI包不是更好嗎?  回復(fù)  更多評(píng)論
            
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2008-05-30 10:03 艷兒
          (*^__^*) 嘻嘻……,好像不用這么麻煩吧!
          直接在jsp頭部加一行,就ok了啊!o(∩_∩)o...哈哈!
          word頁(yè)面只要在jsp頭設(shè)置如下指令:
          <%@page contentType="application/msword;charset=GBK" %>

          excel如下:
          <%@page contentType="application/vnd.ms-excel;charset=GBK" %>

          其他內(nèi)容直接輸出就ok了!
          http://newyaner.blog.sohu.com/84170098.html
            回復(fù)  更多評(píng)論
            
          # 補(bǔ)充 2008-05-30 10:05 艷兒
          導(dǎo)出和打印功能都可以這樣實(shí)現(xiàn)!
          如果要?jiǎng)討B(tài)編輯excel文件就用樓主的jxl吧!  回復(fù)  更多評(píng)論
            
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2008-07-16 22:20 易發(fā)布
          易發(fā)布分類(lèi)信息網(wǎng) http://www.yifabu.cn 引領(lǐng)分類(lèi)信息新時(shí)代 發(fā)布信息,展示產(chǎn)品,投放廣告,我們?yōu)槟氲酶艿剑涣魅海?6041092
          收錄情況:各大搜索引擎收錄良好,百度每日更新快照,各個(gè)關(guān)鍵字表現(xiàn)良好,排名前列,內(nèi)頁(yè)鏈接 http://www.yifabu.cn/link/add.asp  回復(fù)  更多評(píng)論
            
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2008-10-25 01:24 網(wǎng)友
          response.setContentType("application/vnd.ms-excel;charset=gbk");
          我在我的servlet設(shè)置了這個(gè),為什么我彈出的保存窗口,的文件名是亂碼???  回復(fù)  更多評(píng)論
            
          # 用ajax發(fā)送請(qǐng)求,怎么獲取[未登錄](méi) 2008-11-19 20:39 小小
          用ajax發(fā)送請(qǐng)求,怎么獲取  回復(fù)  更多評(píng)論
            
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2009-03-02 14:36 wangdong
          我怎么弄都是亂碼呢!!!
          <%@ page contentType="application/vnd.ms-excel;charset=utf-8"%>
          <%@ page import="org.apache.poi.hssf.*,org.apache.poi.hssf.usermodel.*,java.io.*" %>
          <%
          response.setHeader("Pragma","no-cache");
          response.setHeader("Cache-Control","no-cache");
          response.setDateHeader("Expires",0);
          response.setContentType("application/vnd.ms-excel");
          response.setHeader("Content-disposition","attachment;filename=\"test.xls\"");//定義文件名
          HSSFWorkbook wbb = new HSSFWorkbook();
          HSSFSheet sheet = wbb.createSheet();
          wbb.setSheetName(0, "sheet");
          HSSFRow row = sheet.createRow(0);
          HSSFCell cell1 = row.createCell((short)0);
          cell1.setCellValue("test");
          HSSFCell cell2 = row.createCell((short)1);
          cell2.setCellValue("中文");
          OutputStream ops = response.getOutputStream();
          wbb.write(ops);
          ops.flush();
          ops.close();
          %>  回復(fù)  更多評(píng)論
            
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2009-07-28 15:36 yuwenfang
          能否把引的包都貼出來(lái)呢?呵呵..本人是菜鳥(niǎo),沒(méi)看明白!  回復(fù)  更多評(píng)論
            
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2011-01-13 18:10 yagjie
          我怎么用不上了  回復(fù)  更多評(píng)論
            
          # re: Servlet,JSP 動(dòng)態(tài)生成excel文件并提示下載的簡(jiǎn)單方法 2015-10-29 16:48 阿朱
          我在jsp頁(yè)面請(qǐng)求action 然后樓主方法我是放在action中的 斷點(diǎn)跟蹤方法 至 彈出下載提示框后 一直在循環(huán)調(diào)用 死循環(huán)了
            回復(fù)  更多評(píng)論
            

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 大庆市| 扎囊县| 昆山市| 乌拉特中旗| 陆川县| 沂南县| 门头沟区| 大竹县| 洛扎县| 沧源| 甘孜| 鹿泉市| 丰顺县| 龙川县| 拜泉县| 越西县| 洪湖市| 靖州| 海淀区| 榆中县| 玛纳斯县| 镇康县| 沂水县| 大城县| 碌曲县| 姜堰市| 礼泉县| 青浦区| 合江县| 沭阳县| 云安县| 金平| 金溪县| 靖边县| 高碑店市| 武城县| 东阿县| 罗定市| 阳山县| 宣汉县| 西昌市|