我有一個約16000 X 16000的二維數組,我想將這些記錄導出到Excel文件。目前我可以在短時間內輸出多達1000個X1000的2D陣列。但是,當我增加大小的數組例如3000 X 3000我的程序運行很長時間而不返回任何數據。 我在尋求幫助將整個2D數組導出到excel文件並使用POI。寫一個巨大的數據2D數組,以Excel文件使用Java POI
我的示例代碼來導出數據,其中一個參數是我的二維數組。
公共類exportData {
public static void exportDataToExcel(String fileName, String tabName, int[][] data) throws FileNotFoundException, IOException
{
//Create new workbook and tab
Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream(fileName);
Sheet sheet = wb.createSheet(tabName);
//Create 2D Cell Array
Row[] row = new Row[data.length];
Cell[][] cell = new Cell[row.length][];
//Define and Assign Cell Data from Given
for(int i = 0; i < row.length; i ++)
{
row[i] = sheet.createRow(i);
cell[i] = new Cell[data[i].length];
for(int j = 0; j < cell[i].length; j ++)
{
cell[i][j] = row[i].createCell(j);
cell[i][j].setCellValue(data[i][j]);
}
}
//Export Data
wb.write(fileOut);
fileOut.close();
System.out.println("File exported successfully");
}
}
你嘗試調試找出哪些行的執行被掛起的完整的例子嗎?可能是wb.write(fileOut);? – Maas
我沒有試圖做到這一點,但我正在尋找更好的方式輸出我的整個數組的excel文件的任何建議。 –
如果可能,你應該避免POI和這樣的庫,並直接以CSV格式保存文件。 Excel可以打開這些,轉換它們等。 – EJP