poi从数据库导出百万数据并写入excel

package com.example.demo.bigDataExcelOutWrite;
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;

public class BigDataExcelOutWrite {
	/**
	 * 数据库连接操作
	 * 
	 * @throws Exception
	 */
	public Connection getConnection() throws Exception {
 
		// 使用jdbc链接数据库
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		String url = "jdbc:mysql://localhost:3306/data?serverTimezone=UTC&useUnicode=true&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:/newaccountinfo.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 trading_cardnumber,trading_accountnumber,account_name,account_cardnumber FROM accountinfo WHERE account_name=\"\" GROUP BY trading_cardnumber, account_name,trading_accountnumber,account_cardnumber";
			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("第一行");
				    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("第六行");
				}
				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();
 
			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);
	}
}

  

posted @ 2020-01-02 13:48  黑魔法os  阅读(672)  评论(0编辑  收藏  举报