我需要使用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