zoukankan      html  css  js  c++  java
  • java大数据导出

    excel2003(xls)只支持65536条数据,poi3.9提供了海量数据导出,支持07和10版(xlsx)

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    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.ss.usermodel.Workbook;
    import org.apache.poi.xssf.streaming.SXSSFWorkbook;
    
    /**
     * @Description: poi实现输出信息到excel文件
     * @author dongye
     * @date 2016年8月17日 下午3:09:13
     */
    public class TestMore100 {
    	public static void main(String[] args) throws Exception {
    		TestMore100 tm = new TestMore100();
    		tm.jdbcex(true);
    	}
    
    	public void jdbcex(boolean isClose) throws InstantiationException,
    			IllegalAccessException, ClassNotFoundException, SQLException,
    			IOException, InterruptedException {
    		String xlsFile = "d:/test100.xlsx"; // 输出文件
    		Workbook wb = new SXSSFWorkbook(100); // 创建excel文件,内存只有100条记录【关键语句】
    		Sheet sheet = wb.createSheet("我的第一个工作簿"); // 建立新的sheet对象
    
    		Row nRow = null;
    		Cell nCell = null;
    
    		// 使用jdbc链接数据库
    		Class.forName("com.mysql.jdbc.Driver").newInstance();
    
    		String url = "jdbc:mysql://172.20.10.32:3306/sys?characterEncoding=UTF-8";
    		String user = "root";
    		String password = "root";
    
    		Connection conn = DriverManager.getConnection(url, user, password);
    		Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
    				ResultSet.CONCUR_UPDATABLE);
    
    		String sql = "select name,age,des from customer limit 1000000"; // 100万测试数据
    		ResultSet rs = stmt.executeQuery(sql);
    
    		long startTime = System.currentTimeMillis(); // 开始时间
    		System.out.println("strat execute time: " + startTime);
    		// context
    		int rowNo = 0;
    		int colNo = 0;
    		while (rs.next()) {
    			colNo = 0;
    			nRow = sheet.createRow(rowNo++);
    
    			nCell = nRow.createCell(colNo++);
    			nCell.setCellValue(rs.getString(colNo));
    
    			nCell = nRow.createCell(colNo++);
    			nCell.setCellValue(rs.getString(colNo));
    
    			if (rowNo % 100 == 0) {
    				System.out.println("row no: " + rowNo);
    			}
    
    			Thread.sleep(1); // 休息一下,防止对CPU占用
    		}
    
    		long finishedTime = System.currentTimeMillis(); // 处理完成时间
    		System.out.println("finished execute  time: "
    				+ (finishedTime - startTime) / 1000 + "m");
    
    		FileOutputStream fOut = new FileOutputStream(xlsFile);
    		wb.write(fOut);
    		fOut.flush();
    		fOut.close();
    
    		long stopTime = System.currentTimeMillis(); // 写文件时间
    		System.out.println("write xlsx file time: " + (stopTime - startTime)
    				/ 1000 + "m");
    
    		if (isClose) {
    			this.close(rs, stmt, conn);
    		}
    	}
    
    	// close resource
    	private void close(ResultSet rs, Statement stmt, Connection conn)
    			throws SQLException {
    		rs.close();
    		stmt.close();
    		conn.close();
    	}
    

      

  • 相关阅读:
    h5手机端禁止缩放问题
    element upload 一次性上传多张图片(包含自定义上传不走action)
    vue开发移动端项目 过渡动画问题
    vue中使用transition标签底部导航闪烁问题
    vue element upload图片 转换成base64
    vue项目 sockjs-node一直报错问题
    获得省市区 二级 三级 四级 五级联动数据地址
    vue项目中引入第三方框架
    element中使用button会刷新一遍页面
    [Java] 字符流 Writer,输出字符数据PrintWriter
  • 原文地址:https://www.cnblogs.com/snow1314/p/5780313.html
Copyright © 2011-2022 走看看