1.先創建一個普通的java工程
2.導入jar包
3.編寫java代碼
package com.kaka.test;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
/**
* @Description: 處理百萬級別的excel導出操作工具類
* @author : hujingbo
* @date : 2017-9-22 下午1:55:31
*/
public class BigDataExcelOutWrite {
/**
* 數據庫連接操作
*
* @throws Exception
*/
public Connection getConnection() throws Exception {
// 使用jdbc鏈接數據庫
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/testpo?characterEncoding=UTF-8";
String username = "root";
String password = "root";
// 獲取數據庫連接
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
/**
*
* @Title: WriteExcel
* @Description: 執行導出Excel操作
* @param
* @return boolean
* @throws
*/
public boolean WriteExcel(boolean isClose) {
String excelFile = "D:\\user.xlsx";
// 內存中只創建100個對象,寫臨時文件,當超過100條,就將內存中不用的對象釋放。
SXSSFWorkbook wb = new SXSSFWorkbook(100);
Sheet sheet = null; // 工作表對象
Row nRow = null; // 行對象
Cell nCell = null; // 列對象
try {
Connection conn = getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from user limit 1000";
ResultSet rs = stmt.executeQuery(sql); // 獲取執行結果
ResultSetMetaData rsmd = rs.getMetaData(); // 獲取執行結果的結構(rs.getMetaData().getTableName(1))就可以返回表名,rs.getMetaData().getColumnCount())
long startTime = System.currentTimeMillis();
System.out.println("開始執行時間 : " + startTime / 1000 + "m");
int rowNo = 0; // 總行號
int pageRowNo = 0; // 頁行號
while (rs.next()) {
// 打印300000條後切換到下個工作表,可根據需要自行拓展,2百萬,3百萬...數據一樣操作,只要不超過1048576就可以
if (rowNo % 300000 == 0) {
System.out.println("當前sheet頁爲:" + rowNo / 300000 );
sheet = wb.createSheet("我的第" + (rowNo / 300000 + 1) + "個工作簿");// 建立新的sheet對象
sheet = wb.getSheetAt(rowNo / 300000); // 動態指定當前的工作表
pageRowNo = 1; // 每當新建了工作表就將當前工作表的行號重置爲1
//定義表頭
nRow = sheet.createRow(0);
Cell cel0 = nRow.createCell(0);
cel0.setCellValue("id");
Cell cel2 = nRow.createCell(1);
cel2.setCellValue("姓名");
Cell cel3 = nRow.createCell(2);
cel3.setCellValue("電話");
Cell cel4 = nRow.createCell(3);
cel4.setCellValue("生日");
Cell cel5 = nRow.createCell(4);
cel5.setCellValue("郵箱");
Cell cel6 = nRow.createCell(5);
cel6.setCellValue("did");
}
rowNo++;
nRow = sheet.createRow(pageRowNo++); // 新建行對象
// 打印每行,每行有6列數據 rsmd.getColumnCount()==6 --- 列屬性的個數
for (int i = 0; i < rsmd.getColumnCount(); i++) {
nCell = nRow.createCell(i);
nCell.setCellValue(rs.getString(i + 1));
}
if (rowNo % 10000 == 0) {
System.out.println("row no: " + rowNo);
}
}
long finishedTime = System.currentTimeMillis(); // 處理完成時間
System.out.println("數據讀取完成耗時 : " + (finishedTime - startTime) / 1000 + "m");
FileOutputStream fOut = new FileOutputStream(excelFile);//將數據寫入Excel
wb.write(fOut);
fOut.flush(); // 刷新緩衝區
fOut.close();
// wb.close();// 停止讀寫
long stopTime = System.currentTimeMillis(); // 寫文件時間
System.out.println("數據寫入Excel表格中耗時 : " + (stopTime - startTime) / 1000 + "m");
if (isClose) {
this.close(rs, stmt, conn);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 執行關閉流的操作
private void close(ResultSet rs, Statement stmt, Connection conn)throws SQLException {
rs.close();
stmt.close();
conn.close();
}
//測試
public static void main(String[] args)
{
BigDataExcelOutWrite bdeo = new BigDataExcelOutWrite();
bdeo.WriteExcel(true);
}
}
//結果