如何在jsp中将整个检索的表格数据转换为excel

时间:2015-07-22 16:33:11

标签: java excel jsp hssf

我正在使用此代码,但只有一行表数据输入到文件中..我正在使用mysql数据库。连接在connection.jsp中建立。 tablename是登录。使用poi-2.5.1.jar文件。这段代码工作没有任何错误,但如何将整个表数据转换为excel表

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
         <!DOCTYPE html>
         <html>
                <%@page import="java.sql.*"%>
<%@pageimport="java.util.*,java.io.*,javax.servlet.*,javax.servlet.http.*"%>
         <%@ page import="java.io.*"%>
         <%@include file="connection.jsp" %>
         <%@page import="  org.apache.poi.hssf.usermodel.*"%>
         <head>
            <meta httpequiv="ContentType"content="text/html;charset=UTF-8">
                 <title>JSP Page</title>
             </head>
             <% response.setContentType("application/xls");
                response.setHeader("ContentDisposition","attachment;filename=File.xls"); %>
             <body>
                 <table>
                     <tr><th>CG</th><th>CD</th></tr>
                 <%
                 Statement stmt = conn.createStatement();
          String filename="C:/Users/sangu/Downloads/data.xls" ;
         HSSFWorkbook hwb=new HSSFWorkbook();
         HSSFSheet sheet =  hwb.createSheet("sheet");

         HSSFRow row=   sheet.createRow((short)0);
             ResultSet res = stmt.executeQuery("select * from login ");
             while (res.next())
            {            
                     String cg = res.getString(1);
                 String cd = res.getString(3);
                 row.createCell((short) 0).setCellValue(cd);
         row.createCell((short) 1).setCellValue(cd);
         row.createCell((short) 1).setCellValue(cd);                
                 %>
                 <tr>
                 <td align="center"><%=cg%></td>
                 <td align="center"><%=cd%></td>
                 </tr>
                <%
         FileOutputStream fileOut =  new FileOutputStream(filename);
         hwb.write(fileOut);
         fileOut.close();
         out.println("Your excel file has been generated!");
                        hwb.write(response.getOutputStream()); }
                 %>
                 </table>
             </body>
         </html>

1 个答案:

答案 0 :(得分:0)

不确定但不应该知道 HSSFRow row= sheet.createRow((short)0); 在里面循环?所以它会为每个结果创建新行吗?

编辑:

                     Statement stmt = conn.createStatement();
      String filename="C:/Users/sangu/Downloads/data.xls" ;
     HSSFWorkbook hwb=new HSSFWorkbook();
     HSSFSheet sheet =  hwb.createSheet("sheet");


         ResultSet res = stmt.executeQuery("select * from login ");
         while (res.next())
        {        
             HSSFRow row=   sheet.createRow((short)0);
             String cg = res.getString(1);
             String cd = res.getString(3);
             row.createCell((short) 0).setCellValue(cd);
     row.createCell((short) 1).setCellValue(cd);
     row.createCell((short) 1).setCellValue(cd);                
             %>
             <tr>
             <td align="center"><%=cg%></td>
             <td align="center"><%=cd%></td>
             </tr>
            <%
     FileOutputStream fileOut =  new FileOutputStream(filename);
     hwb.write(fileOut);
     fileOut.close();
     out.println("Your excel file has been generated!");
                    hwb.write(response.getOutputStream()); }