POI3.8中 大数据量的处理

阅读更多

package cn.cy.codeTest;

 

import java.io.FileOutputStream;

 

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.ss.util.CellReference;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;

 

public class XlsMergeUtil {

public static void main(String[] args) throws Throwable {

Workbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory,

// exceeding rows will be

// flushed to disk

Sheet sh = wb.createSheet();

for (int rownum = 0; rownum < 100000; rownum++) {

Row row = sh.createRow(rownum);

for (int cellnum = 0; cellnum < 10; cellnum++) {

Cell cell = row.createCell(cellnum);

String address = new CellReference(cell).formatAsString();

cell.setCellValue(address+":"+rownum);

}

}

FileOutputStream out = new FileOutputStream("c:/sxssf.xlsx");

wb.write(out);

out.close();

}

}

 

package cn.cy.codeTest;

import java.io.FileOutputStream;
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;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class OracleExpertExcel_new {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

	    
		
		Workbook wb = new SXSSFWorkbook(1000);
 
		Sheet xssfsheet = wb.createSheet();
		Connection ct = null;
		Statement ps = null;
		ResultSet rs = null;
		 
	   String sql="SELECT dis.* FROM AR_DISTRIBUTIONS_ALL dis, \n" + 
	     "AR_CASH_RECEIPT_HISTORY_ALL crh \n" + 
	     "WHERE crh.cash_receipt_id = 590065 \n" + 
	     "AND dis.source_table = 'CRH' \n" + 
	     "AND dis.source_id = crh.cash_receipt_history_id \n" + 
	     "UNION SELECT dis.* FROM AR_DISTRIBUTIONS_ALL dis, AR_RECEIVABLE_APPLICATIONS_ALL app \n" + 
	     "WHERE app.cash_receipt_id = 590065 \n" + 
	     "AND dis.source_table = 'RA' \n" + 
	     "AND dis.source_id = app.receivable_application_id \n" + 
	     "ORDER BY 3"; 
		
		//String sql="  SELECT dis.* FROM AR_DISTRIBUTIONS_ALL dis where dis.line_id=1860";
		int i = 0;

		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String conn="jdbc:oracle:thin:@192.168.XX.XX:1529:UAT";
			ct = DriverManager.getConnection(conn, "apps", "XX");
			ps = ct.createStatement();
			rs = ps.executeQuery(sql);
			while (rs.next()) {
				 
				Row xssfrow = xssfsheet.createRow(i);
				
				Cell cell0 = xssfrow.createCell(0); 
				cell0.setCellValue(rs.getString(1));

				Cell cell1 = xssfrow.createCell(1); 
				cell1.setCellValue(rs.getString(2));

				Cell cell2 = xssfrow.createCell(2); 
				cell2.setCellValue(rs.getString(3));

				Cell cell3 = xssfrow.createCell(3); 
				cell3.setCellValue(rs.getString(4));

				Cell cell4 = xssfrow.createCell(4); 
				cell4.setCellValue(rs.getString(5));

				Cell cell5 = xssfrow.createCell(5); 
				cell5.setCellValue(rs.getString(6));

				Cell cell6 = xssfrow.createCell(6); 
				cell6.setCellValue(rs.getString(7));

				Cell cell7 = xssfrow.createCell(7); 
				cell7.setCellValue(rs.getString(8));

				Cell cell8 = xssfrow.createCell(8); 
				cell8.setCellValue(rs.getString(9));

				Cell cell9 = xssfrow.createCell(9); 
				cell9.setCellValue(rs.getString(10));

				Cell cell10 = xssfrow.createCell(10); 
				cell10.setCellValue(rs.getString(11));

				Cell cell11 = xssfrow.createCell(11); 
				cell11.setCellValue(rs.getString(12));

				Cell cell12 = xssfrow.createCell(12); 
				cell12.setCellValue(rs.getString(13));

				Cell cell13 = xssfrow.createCell(13); 
				cell13.setCellValue(rs.getString(14));

				Cell cell14 = xssfrow.createCell(14); 
				cell14.setCellValue(rs.getString(15));

				Cell cell15 = xssfrow.createCell(15); 
				cell15.setCellValue(rs.getString(16));

				Cell cell16 = xssfrow.createCell(16); 
				cell16.setCellValue(rs.getString(17));

				Cell cell17 = xssfrow.createCell(17); 
				cell17.setCellValue(rs.getString(18));

				Cell cell18 = xssfrow.createCell(18); 
				cell18.setCellValue(rs.getString(19));

				Cell cell19 = xssfrow.createCell(19); 
				cell19.setCellValue(rs.getString(20));

				Cell cell20 = xssfrow.createCell(20); 
				cell20.setCellValue(rs.getString(21));

				Cell cell21 = xssfrow.createCell(21); 
				cell21.setCellValue(rs.getString(22));

				Cell cell22 = xssfrow.createCell(22); 
				cell22.setCellValue(rs.getString(23));

				Cell cell23 = xssfrow.createCell(23); 
				cell23.setCellValue(rs.getString(24));

				Cell cell24 = xssfrow.createCell(24); 
				cell24.setCellValue(rs.getString(25));

				Cell cell25 = xssfrow.createCell(25); 
				cell25.setCellValue(rs.getString(26));

				Cell cell26 = xssfrow.createCell(26); 
				cell26.setCellValue(rs.getString(27));

				Cell cell27 = xssfrow.createCell(27); 
				cell27.setCellValue(rs.getString(28));

				Cell cell28 = xssfrow.createCell(28); 
				cell28.setCellValue(rs.getString(29));

				Cell cell29 = xssfrow.createCell(29); 
				cell29.setCellValue(rs.getString(30));

				Cell cell30 = xssfrow.createCell(30); 
				cell30.setCellValue(rs.getString(31));

				Cell cell31 = xssfrow.createCell(31); 
				cell31.setCellValue(rs.getString(32));

				Cell cell32 = xssfrow.createCell(32); 
				cell32.setCellValue(rs.getString(33));

				Cell cell33 = xssfrow.createCell(33); 
				cell33.setCellValue(rs.getString(34));

				Cell cell34 = xssfrow.createCell(34); 
				cell34.setCellValue(rs.getString(35));

				Cell cell35 = xssfrow.createCell(35); 
				cell35.setCellValue(rs.getString(36));

				Cell cell36 = xssfrow.createCell(36); 
				cell36.setCellValue(rs.getString(37));

				Cell cell37 = xssfrow.createCell(37); 
				cell37.setCellValue(rs.getString(38));

				Cell cell38 = xssfrow.createCell(38); 
				cell38.setCellValue(rs.getString(39));

				Cell cell39 = xssfrow.createCell(39); 
				cell39.setCellValue(rs.getString(40));

				Cell cell40 = xssfrow.createCell(40); 
				cell40.setCellValue(rs.getString(41));

				Cell cell41 = xssfrow.createCell(41); 
				cell41.setCellValue(rs.getString(42));

				Cell cell42 = xssfrow.createCell(42); 
				cell42.setCellValue(rs.getString(43));

				Cell cell43 = xssfrow.createCell(43); 
				cell43.setCellValue(rs.getString(44)+"44");

 
				i++;

			}
			System.out.println("i="+i);

		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally {

			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (ct != null) {
					ct.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	 
	    FileOutputStream out = new FileOutputStream("c:/sxssf.xlsx");
		wb.write(out);
		out.close();
		 
		System.out.println("导出成功");
		
	
	}

}

 

你可能感兴趣的:(POI3.8中 大数据量的处理)