Chinaunix首页 | 论坛 | 博客
  • 博客访问: 40818
  • 博文数量: 28
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-17 11:05
文章分类
文章存档

2015年(28)

我的朋友

分类: Java

2015-04-18 10:30:11

从数据库导出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. }

阅读(642) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~