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);
}
}
//结果