mysql数据库导出百万数据到excel_poi从数据库导出百万数据并写入excel

该博客展示了如何使用Java的Apache POI库,特别是SXSSFWorkbook,来高效地从MySQL数据库导出百万条数据并写入Excel文件。通过创建内存中只保留100个对象的SXSSFWorkbook,实现了大文件的分批写入,避免了内存溢出。代码示例中包含了数据库连接、SQL查询、数据读取和Excel文件的创建与写入过程。
摘要由CSDN通过智能技术生成

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);

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值