怎么在JSP里做使显示的数据表导出EXCEL

package   com.hull.servlet;  
   
  import   jxl.*;  
  import   jxl.write.*;  
  import   jxl.format.BorderLineStyle;  
  import   java.io.*;  
  import   javax.servlet.http.*;  
  import   javax.servlet.*;  
  import   java.sql.ResultSet;  
  import   java.sql.SQLException;  
   
  import   com.hull.db.pool.SqlServerPool;  
  import   com.hull.tools.StringConverter;  
   
  /**  
    *   <p>Title:   hull</p>  
    *   <p>Description:   read   and   write   excel   with   java</p>  
    *   <p>Copyright:   Copyright   (c)   2004</p>  
    *   <p>Company:   hz-group</p>  
    *   @author   wangdong  
    *   @version   1.0  
    */  
   
  public   class   JExcel   extends   HttpServlet{  
      private   String   excelFilePath;  
      private   WritableWorkbook   wbbook;  
      //private   FileOutputStream   os;  
      private   OutputStream   os;  
      private   WritableCellFormat   wcf,wcf_title;  
      private   WritableSheet   ws;  
       
      private   String   title;  
      private   int   sheetid;  
      private   long   shipid;  
       
       
      public   void   init(ServletConfig   config)   throws   ServletException{  
          super.init();  
          excelFilePath   =   config.getInitParameter("excelFilePath");  
          if(excelFilePath   ==   null){  
              throw   new   ServletException("不能创建excel文件!");  
          }  
      }    
       
      public   void   doGet(HttpServletRequest   request,HttpServletResponse   response)  
          throws   ServletException,IOException{  
          response.setContentType("application/vnd.ms-excel");  
          title   =   request.getParameter("shipname");  
          shipid   =   (new   Long(request.getParameter("shipid"))).longValue();  
           
          os   =   response.getOutputStream();  
           
          wbbook   =   Workbook.createWorkbook(os);  
           
          sheetid   =   (wbbook.getSheets()).length;  
           
          try{  
              wcf   =   new   WritableCellFormat();  
              wcf.setAlignment(Alignment.RIGHT);  
              wcf.setBorder(Border.ALL,BorderLineStyle.THIN);  
              wcf.setWrap(true);  
               
              wcf_title   =   new   WritableCellFormat();  
              wcf_title.setAlignment(Alignment.CENTRE);  
              wcf_title.setBorder(Border.ALL,BorderLineStyle.THIN);  
          }  
          catch(WriteException   e){  
          }  
           
          ws   =   wbbook.createSheet(StringConverter.EnToChinese(title),sheetid+1);  
          ws.setColumnView(0,20);  
          ws.setColumnView(1,20);  
          ws.setColumnView(2,20);  
          ws.setColumnView(3,20);  
          try{  
              /**  
                *   写标题  
                */  
              jxl.write.Label   labelTitle   =   new   jxl.write.Label(0,0,StringConverter.EnToChinese(title),wcf_title);  
              ws.mergeCells(0,0,3,0);  
              ws.addCell(labelTitle);  
              /**  
                *   写入船型内容  
                */  
               
              SqlServerPool   pool   =   new   SqlServerPool();  
          int   i=0,j=1,work=1;  
          String   strsql   =   "select   a.*,b.refvalue   from   shipref   a   left   join   refcontent   b   on   b.refid=a.id   "+  
                                          "where   a.shipid="+shipid+"   order   by   a.reftype   desc";  
           
              ResultSet   rs   =   pool.executeQuery(strsql);  
              while(rs.next()){  
               
                  if(rs.getString("reftype").equals("文本")){  
                      jxl.write.Label   typeLabel   =   new   jxl.write.Label(0,j++,rs.getString("refname"),wcf);  
                      ws.addCell(typeLabel);  
                      jxl.write.Label   valueLabel   =   new   jxl.write.Label(1,j-1,rs.getString("refvalue"),wcf);  
                      ws.mergeCells(1,j,3,j);  
                      ws.addCell(valueLabel);  
                  }  
                  else{  
                  if(i%2   ==   1){  
                      jxl.write.Label   typeLabel   =   new   jxl.write.Label(0,j,rs.getString("refname"),wcf);  
                      ws.addCell(typeLabel);  
                      jxl.write.Label   valueLabel   =   new   jxl.write.Label(1,j,rs.getString("refvalue"),wcf);  
                      ws.addCell(valueLabel);  
                  }  
                  else{  
                      jxl.write.Label   typeLabel   =   new   jxl.write.Label(2,j++,rs.getString("refname"),wcf);  
                      ws.addCell(typeLabel);  
                      jxl.write.Label   valueLabel   =   new   jxl.write.Label(3,j-1,rs.getString("refvalue"),wcf);  
                      ws.addCell(valueLabel);  
                  }  
                  work   =   j;  
                  }  
                  i++;  
              }  
               
              if(work%2   ==   1){  
                  jxl.write.Label   additional1   =   new   jxl.write.Label(2,work,"",wcf);  
                  ws.addCell(additional1);  
                  jxl.write.Label   additional2   =   new   jxl.write.Label(3,work,"",wcf);  
                  ws.addCell(additional2);  
              }  
               
              wbbook.write();  
              wbbook.close();  
              os.flush();  
              os.close();  
          }  
          catch(Exception   ex){  
          }  
      }  
  }

你可能感兴趣的:(jsp,Excel,OS,J#,WCF)