导出数据库数据到Excel

系统 1990 0

做了一个利用POI导出数据库数据到Excel的例子,并把这个例子与大家一起分享,一起学习,共同进步!
先简单介绍一下poi,poi是apache组织的一个开源项目,最新版为poi-3.0.1,要使用poi需要到apache网站下载poi-bin3.0.1.zip压缩包,并将poi-3.0.1.jar加入到项目的ClassPath中。

下面是完成这个实例的步骤:

1、建立好一个web工程:poi;

2、导入所需要的jar包:poi-3.0.1.jar和mysql数据库驱动包;

3、编写一个StudentResultSet类,用于取出数据库中的数据;

Java代码
  1. package org.hnylj.poi;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. public class StudentResultSet{
  8. private Connectionconn;
  9. private PreparedStatementpstmt;
  10. private ResultSetrs;
  11. private static final StringDRIVER= "com.mysql.jdbc.Driver" ;
  12. private static final StringURL= "jdbc:mysql://localhost:3306/poi" ;
  13. private static final StringUSERNAME= "root" ;
  14. private static final StringPASSWORD= "123" ;
  15. //查询数据库中的数据
  16. public ResultSetquery(){
  17. try {
  18. Class.forName(DRIVER);
  19. conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
  20. pstmt=conn.prepareStatement( "select*fromstudent" );
  21. rs=pstmt.executeQuery();
  22. } catch (ClassNotFoundExceptione){
  23. e.printStackTrace();
  24. } catch (SQLExceptione){
  25. e.printStackTrace();
  26. }
  27. return rs;
  28. }
  29. }

4、编写一个BuildExcelServlet,用于生成Excel;

Java代码
  1. package org.hnylj.poi;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  12. import org.apache.poi.hssf.usermodel.HSSFRow;
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. public class BuildExcelServlet extends HttpServlet{
  16. protected void doGet(HttpServletRequestrequest,
  17. HttpServletResponseresponse) throws ServletException,IOException{
  18. StudentResultSetstuResultSet= new StudentResultSet();
  19. ResultSetrs=stuResultSet.query();
  20. StringxlsName= "test.xls" ;
  21. StringsheetName= "sheetName" ;
  22. HSSFWorkbookworkbook= new HSSFWorkbook();
  23. HSSFSheetsheet=workbook.createSheet();
  24. workbook.setSheetName( 0 ,sheetName);
  25. HSSFRowrow=sheet.createRow(( short ) 0 );
  26. HSSFCellcell;
  27. try {
  28. ResultSetMetaDatamd=rs.getMetaData();
  29. int nColumn=md.getColumnCount();
  30. for ( int i= 1 ;i<=nColumn;i++){
  31. cell=row.createCell(( short )(i- 1 ));
  32. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  33. cell.setCellValue( new HSSFRichTextString(md.getColumnLabel(i)));
  34. }
  35. int iRow= 1 ;
  36. while (rs.next()){
  37. row=sheet.createRow(( short )iRow);
  38. ;
  39. for ( int j= 1 ;j<=nColumn;j++){
  40. cell=row.createCell(( short )(j- 1 ));
  41. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  42. cell.setCellValue( new HSSFRichTextString(rs.getObject(j)
  43. .toString()));
  44. }
  45. iRow++;
  46. }
  47. Stringfilename=request.getRealPath( "/" )+xlsName;
  48. request.setAttribute( "filename" ,filename);
  49. FileOutputStreamfOut= new FileOutputStream(filename);
  50. workbook.write(fOut);
  51. fOut.flush();
  52. fOut.close();
  53. request.getRequestDispatcher( "OpenExcelServlet" ).forward(request,
  54. response);
  55. } catch (Exceptione){
  56. e.printStackTrace();
  57. }
  58. }
  59. protected void doPost(HttpServletRequestrequest,
  60. HttpServletResponseresponse) throws ServletException,IOException{
  61. this .doGet(request,response);
  62. }
  63. }

5、编写一个OpenExcelServlet,用于把生成的Excel在页面上打开;

Java代码
  1. package org.hnylj.poi;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. public class OpenExcelServlet extends HttpServlet{
  11. public void doGet(HttpServletRequestrequest,HttpServletResponseresponse)
  12. throws ServletException,IOException{
  13. response.setContentType( "application/vnd.ms-excel" );
  14. PrintWriterout=response.getWriter();
  15. StringfilePath=(String)request.getAttribute( "filename" );
  16. BufferedInputStreambis= null ;
  17. try {
  18. bis= new BufferedInputStream( new FileInputStream(filePath));
  19. int bytesRead;
  20. while ((bytesRead=bis.read())!=- 1 ){
  21. out.write(bytesRead);
  22. out.flush();
  23. }
  24. } catch (IOExceptione){
  25. e.printStackTrace();
  26. } finally {
  27. if (bis!= null )
  28. bis.close();
  29. if (out!= null )
  30. out.close();
  31. }
  32. }
  33. public void doPost(HttpServletRequestrequest,HttpServletResponseresponse)
  34. throws ServletException,IOException{
  35. this .doGet(request,response);
  36. }
  37. }

6、在web.xml里配置一下servlet的映射;

Xml代码
  1. < servlet >
  2. < servlet-name > BuildExcelServlet </ servlet-name >
  3. < servlet-class > org.hnylj.poi.BuildExcelServlet </ servlet-class >
  4. </ servlet >
  5. < servlet >
  6. < servlet-name > OpenExcelServlet </ servlet-name >
  7. < servlet-class > org.hnylj.poi.OpenExcelServlet </ servlet-class >
  8. </ servlet >
  9. < servlet-mapping >
  10. < servlet-name > OpenExcelServlet </ servlet-name >
  11. < url-pattern > /OpenExcelServlet </ url-pattern >
  12. </ servlet-mapping >
  13. < servlet-mapping >
  14. < servlet-name > BuildExcelServlet </ servlet-name >
  15. < url-pattern > /BuildExcelServlet </ url-pattern >
  16. </ servlet-mapping >

7、启动tomcat,运行程序

导出数据库数据到Excel


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论