怎么样才能让网页的自动生成一个excel,或者,在数据库里面读取数据,自动生成excel呢。
最好提供一个完整的代码。

解决方案 »

  1.   

    一般有两种实现:
    1、把网页上显示的内容直接输出为excel,这种方式实现起来比较简单,类似于,在浏览器中ctrl+a,然后到excel中ctrl+v。所以用这种方式时,你的web页面应该做的尽量简单,比如就是单纯的一个table,加上黑色边框,中间是内容。这样生成的excel就比较标准了。2、使用一些开源的操作excel的API,来读取数据库,然后在服务器端生成excel文件,最后将文件发送到客户端让用户下载。这种方式比较复杂一些,你需要写不少的程序来读取数据生成excel,还要发送到客户端。
      

  2.   

    http://community.csdn.net/Expert/topic/4719/4719998.xml?temp=.7090418
      

  3.   

    如果你选择后者,使用poi,参见http://jfish.blog.ccidnet.com/blog/ccid/do_showone/tid_18396.html
    如果是前者,等下我贴示例代码
      

  4.   

    IceCraft(心淡情浓)我的你。呵呵~~谢谢了。
      

  5.   

    IceCraft(心淡情浓)我等你替代码
    谢谢了。
    刚刚打错字了
      

  6.   

    主要是改变Web页面流的格式即可,实例代码如下:
    <%@ page contentType="text/html;charset=gb2312"%>
    <%
    String str=request.getParameter("submit");
    if(str==null){
     str="";
    }
    if (str.equals("yes")){
     response.setHeader("Content-disposition","inline; filename=test.xls");
     response.setContentType("application/msexcel;charset=GB2312");
    }
    %>
    <html>
    <head>
    <title>html2doc</title>
    </head>
    <body>
    <p>测试html to doc
    <table border="1" width="300">
    <tr><td bgcolor="red" widht="100">测试</td><td>你好</td></tr>
    <tr><td bgcolor="blue">333</td><td>444</td></tr>
    </table>
    <%
    if (!str.equals("yes")){
    %>
    <p>将当前的页面保存为WORD吗?
    <form method="get" name="form">
    <input type ="submit" value ="yes" name="submit">
    </form>
    <%
    }
    %>
    </body>
    </html>这个例子是输出excel格式的,如果要输出word格式则修改msexcel为msword,同时文件扩展名xls修改为doc即可,其他类型文件类似修改。
      

  7.   

    推荐poi,java实现.HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet1 = wb.createSheet("new sheet");
        HSSFSheet sheet2 = wb.createSheet("second sheet");
        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();
      

  8.   

    顶 IceCraft(心淡情浓) 
    我用的也是这种方法~~比较简单好用