随笔 - 8  文章 - 24  trackbacks - 0
<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(4)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

1.下载jxl.rar包

项目地址:http://www.andykhan.com/jexcelapi/
下载地址:http://www.andykhan.com/jexcelapi/download.html

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


    2.把包放到WEB-INF的lib目录下在开发环境中引入这个包

    3.开始写代码了,这里以一个Struts1.2的ActionMethod为例,其实只要能取了request和response对象,操作都是一样的的。

     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        //读出数据

    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//一次最多写入量

    25            int times = (ncout+maxnum-1)/maxnum;
    26
                
    27            //大循环

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

    31                WritableSheet wsheet = wwb.createSheet("members_"+(t+1),t);
    32                //设置表头

    33                Label label = new Label(0,0,"");
    34
                    wsheet.addCell(label);
    35                label = new Label(0,0,"会员姓名"
    );
    36
                    wsheet.addCell(label);
    37                label = new Label(1,0,"卡号"
    );
    38
                    wsheet.addCell(label);
    39                label = new Label(2,0,"联系地址"
    );
    40
                    wsheet.addCell(label);
    41                label = new Label(3,0,"邮编"
    );
    42
                    wsheet.addCell(label);
    43                label = new Label(4,0,"联系电话"
    );
    44
                    wsheet.addCell(label);
    45                label = new Label(5,0,"手机"
    );
    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                //读出数据

    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            }
    //结束大循环
    77            
    78
                wwb.write();
    79
                wwb.close();
    80
                os.close();
    81
                response.flushBuffer();
    82
                
    83        }
    catch(Exception e){
    84            System.out.println("生成信息表(Excel格式)时出错:"
    );
    85
                e.printStackTrace();
    86        }

    87        
    88        return null
    ;
    89    }


    代码简单说明:
    1.设定好response的相关属性:
    response.setContentType("application/vnd.ms-excel");
    response.addHeader("Content-Disposition","attachment;   filename=\""   +  Common.getFileName()+".xls"  +   "\""); 
    2.取到response的OutputStream实例,并用这个实例化一个WritableWorkbook对象
    OutputStream os = response.getOutputStream();
    WritableWorkbook wwb = Workbook.createWorkbook(os);
    3.新建一个表
    WritableSheet wsheet = wwb.createSheet("members_"+(t+1),t);
    4.往表里加行头
    Label label = new Label(0,0,"");
        wsheet.addCell(label);
        label = new Label(0,0,"会员姓名");
        wsheet.addCell(label);
        label = new Label(1,0,"卡号");
        wsheet.addCell(label);
        label = new Label(2,0,"联系地址");
        wsheet.addCell(label);
        label = new Label(3,0,"邮编");
        wsheet.addCell(label);
        label = new Label(4,0,"联系电话");
        wsheet.addCell(label);
        label = new Label(5,0,"手机");
        wsheet.addCell(label);
        label = new Label(6,0,"Email");
        wsheet.addCell(label);
        label = new Label(7,0,"性别");
        wsheet.addCell(label);
    5.往表里加数据行
    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数据输出到response的OutputStream
    wwb.write();
       wwb.close();
       os.close();
       response.flushBuffer();
    7.完成

    有什么疑问可以留言,我会尽量帮助。

  • posted on 2007-09-20 14:47 Vincent.Yu 阅读(12486) 评论(10)  编辑  收藏

    FeedBack:
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 2007-09-27 19:30 千里冰封
    呵呵,POI包不是更好吗?  回复  更多评论
      
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 2008-05-30 10:03 艳儿
    (*^__^*) 嘻嘻……,好像不用这么麻烦吧!
    直接在jsp头部加一行,就ok了啊!o(∩_∩)o...哈哈!
    word页面只要在jsp头设置如下指令:
    <%@page contentType="application/msword;charset=GBK" %>

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

    其他内容直接输出就ok了!
    http://newyaner.blog.sohu.com/84170098.html
      回复  更多评论
      
    # 补充 2008-05-30 10:05 艳儿
    导出和打印功能都可以这样实现!
    如果要动态编辑excel文件就用楼主的jxl吧!  回复  更多评论
      
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 2008-07-16 22:20 易发布
    易发布分类信息网 http://www.yifabu.cn 引领分类信息新时代 发布信息,展示产品,投放广告,我们为您想得更周到,交流群:56041092
    收录情况:各大搜索引擎收录良好,百度每日更新快照,各个关键字表现良好,排名前列,内页链接 http://www.yifabu.cn/link/add.asp  回复  更多评论
      
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 2008-10-25 01:24 网友
    response.setContentType("application/vnd.ms-excel;charset=gbk");
    我在我的servlet设置了这个,为什么我弹出的保存窗口,的文件名是乱码???  回复  更多评论
      
    # 用ajax发送请求,怎么获取[未登录] 2008-11-19 20:39 小小
    用ajax发送请求,怎么获取  回复  更多评论
      
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 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();
    %>  回复  更多评论
      
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 2009-07-28 15:36 yuwenfang
    能否把引的包都贴出来呢?呵呵..本人是菜鸟,没看明白!  回复  更多评论
      
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 2011-01-13 18:10 yagjie
    我怎么用不上了  回复  更多评论
      
    # re: Servlet,JSP 动态生成excel文件并提示下载的简单方法 2015-10-29 16:48 阿朱
    我在jsp页面请求action 然后楼主方法我是放在action中的 断点跟踪方法 至 弹出下载提示框后 一直在循环调用 死循环了
      回复  更多评论
      

    只有注册用户登录后才能发表评论。


    网站导航: