我需要使用Apache POI將600-700K記錄寫入xlsx文件。 我目前使用的代碼是:性能:使用Java,Apache-POI將Oracle ResultSet寫入XLSX
public void writeRecords(ResultSet rs) {
try{
SXSSFWorkbook wb = new SXSSFWorkbook();
wb.setCompressTempFiles(true);
SXSSFSheet sh = (SXSSFSheet)wb.createSheet("Sheet 1");
Row row = null;
int numColumns = rs.getMetaData().getColumnCount();
// Workbook wb = ExcelFileUtil.createExcelWorkBook(true, 5);
sh.setRandomAccessWindowSize(100);// keep 100 rows in memory, exceeding rows will be flushed to disk
Row heading = sh.createRow(1);
ResultSetMetaData rsmd = rs.getMetaData();
for(int x = 0; x < numColumns; x++) {
Cell cell = heading.createCell(x+1);
cell.setCellValue(rsmd.getColumnLabel(x+1));
}
int rowNumber = 2;
int sheetNumber = 0;
while(rs.next()) {
row = sh.createRow(rowNumber);
for(int y = 0; y < numColumns; y++) {
row.createCell(y+1).setCellValue(rs.getString(y+1));
// wb.write(bos);
}
rowNumber++;
}
FileOutputStream out = new FileOutputStream("C:/Users/test1.xlsx");
wb.write(out);
out.close();
}
catch (Exception e){
e.printStackTrace();
}
它工作正常,但它走約50分鐘的時間寫〜65K的記錄。65分鐘記錄的結果集在5-6分鐘內被提取。
有什麼辦法可以在10-15分鐘內用POI寫出600,000-700,000條記錄 。 我們無法將數據導出爲CSV格式,因爲最終用戶只能導入xlsx文件。 關於, Tushar
運行你怎麼知道的ResultSet在7秒內是牽強?在上面的代碼中,您仍然在寫入行時進行提取。 – Thilo
另外,如果Excel將700k行的電子表格放在電子表格中,Excel的行爲如何?那個文件有多大? – Thilo
我之前和之後記錄的時間戳:\t ps = con.prepareStatement(「select * from table_1 where rownum <65000」); \t \t rs = ps.executeQuery(); – Tushar