關於 jsp servlet 將Excel表格內容寫進數據庫,將數據庫導出到Excel中。(二)

從數據庫導出EXCEL。同樣添加poi.jar包。
jsp如下:

點擊(此處)摺疊或打開

  1. <a href="ReportServlet" onclick="return confirm('確認數據導出到E:/下?');">導出數據到Excel</a>
servlet如下:

點擊(此處)摺疊或打開

  1. package control;

  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.sql.Connection;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;

  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import org.apache.poi.hssf.usermodel.HSSFCell;
  14. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  15. import org.apache.poi.hssf.usermodel.HSSFRow;
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  18. import db.DB;

  19. public class ReportServlet extends HttpServlet {

  20.     
  21.     /**
  22.      *
  23.      */
  24.     private static final long serialVersionUID = 1L;
  25.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  26.      throws ServletException, IOException {
  27.      this.doPost(request, response);
  28.      }
  29.      public void doPost(HttpServletRequest request, HttpServletResponse response)
  30.      throws ServletException, IOException {
  31.      request.setCharacterEncoding("utf-8");
  32.      //獲得要生成Excel數據的數據庫中的表名稱
  33.      String tableName = request.getParameter("tableName");
  34.     
  35.      // 禁止數據緩存。
  36.      response.setHeader("Pragma", "no-cache");
  37.      response.setHeader("Cache-Control", "no-cache");
  38.      response.setDateHeader("Expires", 0);
  39.      Connection conn = null ;
  40.      DB db = new DB() ;
  41.      conn = db.getDB() ;
  42.      String sql = "select * from test_table" ;
  43.     
  44.      ResultSet rs = null;
  45.     try {
  46.         rs = conn.createStatement().executeQuery(sql);
  47.     } catch (SQLException e1) {
  48.         // TODO Auto-generated catch block
  49.         e1.printStackTrace();
  50.     }
  51.      // 獲取總列數
  52.      int CountColumnNum=0;
  53.     try {
  54.         CountColumnNum = rs.getMetaData().getColumnCount();
  55.     } catch (SQLException e1) {
  56.         // TODO Auto-generated catch block
  57.         e1.printStackTrace();
  58.     }
  59.      int i = 1 ;
  60.      // 創建Excel文檔
  61.      HSSFWorkbook wb = new HSSFWorkbook() ;
  62.      // sheet 對應一個工作頁
  63.      HSSFSheet sheet = wb.createSheet("test_table表中的數據") ;
  64.      HSSFRow firstrow = sheet.createRow(0); //下標爲0的行開始
  65.      HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
  66.      String[] names = new String[CountColumnNum];
  67.      names[0] = "ID";
  68.      names[1] = "學號";
  69.      names[2] = "姓名";
  70.     
  71.      for(int j= 0 ;j<CountColumnNum; j++){
  72.      firstcell[j] = firstrow.createCell(j);
  73.      firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
  74.      }
  75.      try {
  76.         while(rs.next())
  77.          {
  78.          // 創建電子表格的一行
  79.          HSSFRow row = sheet.createRow(i) ; // 下標爲1的行開始
  80.          for(int j=0;j<CountColumnNum;j++)
  81.          {
  82.          // 在一行內循環
  83.          HSSFCell cell = row.createCell( j) ;
  84.          // 設置表格的編碼集,使支持中文
  85.          //
  86.          // 先判斷數據庫中的數據類型
  87.          // 將結果集裏的值放入電子表格中
  88.          try {
  89.             cell.setCellValue(new HSSFRichTextString(rs.getString(j+1))) ;
  90.         } catch (SQLException e) {
  91.             // TODO Auto-generated catch block
  92.             e.printStackTrace();
  93.         }
  94.          }
  95.          i++ ;
  96.          }
  97.     } catch (SQLException e1) {
  98.         // TODO Auto-generated catch block
  99.         e1.printStackTrace();
  100.     }
  101.      // 創建文件輸出流,準備輸出電子表格
  102.      OutputStream out = new FileOutputStream("E:\\test.xls") ;
  103.      wb.write(out) ;
  104.      out.close() ;
  105.      System.out.println("數據庫導出成功") ;
  106.      try {
  107.         rs.close() ;
  108.     } catch (SQLException e) {
  109.         // TODO Auto-generated catch block
  110.         e.printStackTrace();
  111.     }
  112.      try {
  113.         conn.close() ;
  114.     } catch (SQLException e) {
  115.         // TODO Auto-generated catch block
  116.         e.printStackTrace();
  117.     }
  118.     response.sendRedirect("test.jsp");
  119.      }
  120.      public static void main(String[] args)
  121.      {
  122.      try {
  123.      @SuppressWarnings("unused")
  124.      ReportServlet excel = new ReportServlet() ;
  125.      } catch (Exception e) {
  126.      // TODO Auto-generated catch block
  127.      e.printStackTrace();
  128.      }
  129.      }

  130. }

閱讀(62) | 評論(0) | 轉發(0) |
給主人留下些什麼吧!~~
評論熱議
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章