百万记录poi导出日记

来源:互联网 发布:php做九九乘法表 编辑:程序博客网 时间:2024/05/16 11:56

机器环境:
hp probook 4326s 4G AMD P340 2.20Hz
mysql 5.0.26
sts spring eclipse 
jdk 1.7.09 
tomcat 7.0

步骤:
JDBC访问
使用POI中的 XFFX 对象

导入jar包:
核心包:
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
依赖包:
commons-beanutils-1.7.0.jar
commons-collections-3.0.jar
commons-lang-2.0.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
standard-1.0.2.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
mysql-connector-java-5.1.10-bin.jar
junit-4.8.2.jar
测试程序,从测试表中读取100万数据,轻松写入excel中。

 

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package test;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.sql.Connection;  
  6. import java.sql.DriverManager;  
  7. import java.sql.ResultSet;  
  8. import java.sql.SQLException;  
  9. import java.sql.Statement;  
  10.   
  11. import org.apache.poi.ss.usermodel.Cell;  
  12. import org.apache.poi.ss.usermodel.CellStyle;  
  13. import org.apache.poi.ss.usermodel.Font;  
  14. import org.apache.poi.ss.usermodel.Row;  
  15. import org.apache.poi.ss.usermodel.Sheet;  
  16. import org.apache.poi.ss.usermodel.Workbook;  
  17. import org.apache.poi.xssf.streaming.SXSSFWorkbook;  
  18. import org.apache.poi.xssf.usermodel.XSSFCell;  
  19. import org.apache.poi.xssf.usermodel.XSSFCellStyle;  
  20. import org.apache.poi.xssf.usermodel.XSSFFont;  
  21. import org.apache.poi.xssf.usermodel.XSSFRow;  
  22. import org.apache.poi.xssf.usermodel.XSSFSheet;  
  23. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  24.   
  25. /**  
  26.  * @Description:  poi实现输出信息到excel文件  
  27.  * @Author: nutony  
  28.  * @Date: 2013-05-15  
  29.  */  
  30. public class TestMore100 {  
  31.         public static void main(String[] args) throws Exception {  
  32.                 TestMore100 tm = new TestMore100();  
  33.                 tm.jdbcex(true);  
  34.         }  
  35.   
  36.         public void jdbcex(boolean isClose) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException, InterruptedException {  
  37.                 String xlsFile = "d:/test100.xlsx";                                        //输出文件  
  38.                 Workbook wb = new SXSSFWorkbook(100);                                //创建excel文件,内存只有100条记录【关键语句】  
  39.                 Sheet sheet = wb.createSheet("我的第一个工作簿");                //建立新的sheet对象  
  40.   
  41.                 Row nRow = null;  
  42.                 Cell nCell   = null;  
  43.   
  44.                 //使用jdbc链接数据库  
  45.                 Class.forName("com.mysql.jdbc.Driver").newInstance();    
  46.                   
  47.                 String url = "jdbc:mysql://localhost:3306/jkmore100?characterEncoding=UTF-8";  
  48.                 String user = "root";  
  49.                 String password = "root";  
  50.                   
  51.                 Connection conn = DriverManager.getConnection(url, user,password);     
  52.                 Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);     
  53.   
  54.                 String sql = "select name,age,des from customer limit 1000000";   //100万测试数据  
  55.                 ResultSet rs = stmt.executeQuery(sql);    
  56.                   
  57.                   
  58.                 long  startTime = System.currentTimeMillis();        //开始时间  
  59.                 System.out.println("strat execute time: " + startTime);  
  60.                 //context  
  61.                 int rowNo = 0;  
  62.                 int colNo = 0;  
  63.                 while(rs.next()) {  
  64.                         colNo = 0;  
  65.                         nRow = sheet.createRow(rowNo++);  
  66.   
  67.                         nCell = nRow.createCell(colNo++);  
  68.                         nCell.setCellValue(rs.getString(colNo));  
  69.                           
  70.                         nCell = nRow.createCell(colNo++);  
  71.                         nCell.setCellValue(rs.getString(colNo));  
  72.                           
  73.                         if(rowNo%100==0){  
  74.                                 System.out.println("row no: " + rowNo);  
  75.                         }  
  76.                           
  77.                         Thread.sleep(1);                        //休息一下,防止对CPU占用  
  78.                 }  
  79.                   
  80.                 long finishedTime = System.currentTimeMillis();        //处理完成时间  
  81.                 System.out.println("finished execute  time: " + (finishedTime - startTime)/1000 + "m");  
  82.                   
  83.                   
  84.                 FileOutputStream fOut = new FileOutputStream(xlsFile);  
  85.                 wb.write(fOut);  
  86.                 fOut.flush();  
  87.                 fOut.close();  
  88.                   
  89.                 long stopTime = System.currentTimeMillis();                //写文件时间  
  90.                 System.out.println("write xlsx file time: " + (stopTime - startTime)/1000 + "m");  
  91.                   
  92.                 if(isClose){  
  93.                         this.close(rs, stmt, conn);  
  94.                 }  
  95.         }  
  96.           
  97.         //close resource  
  98.         private void close(ResultSet rs, Statement stmt, Connection conn ) throws SQLException{  
  99.                 rs.close();     
  100.                 stmt.close();     
  101.                 conn.close();   
  102.         }          
  103.           
  104. }  


 

0 0
原创粉丝点击