我需要在 xls 文件(Microsoft 97-xp)中编写超过 1,000,000 行的结果集。所以我正在尝试使用 JExcel API 但我收到错误 OutOfMemoryError: Java heap space 。除了增加JVM内存或创建不同的excel文件然后手动合并它们之外,我如何解决这个问题。我什至不想创建 CSV 文件。请帮忙。
示例代码:
int sheetNumber = 1;
int maxSheetSize=65000;
int start = 0;
int end=maxSheetSize;
int totalsize=1000000;
int completed = 0;
int columnCount=10;
WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));
while (completed < totalsize) {
WritableSheet sheet = workbook.createSheet("Sheet " + (sheetNumber++), 0);
for (int r = start; r < end; r++) {
int i=1;
for (int c = 0; c < columnCount; c++) {
Label label = new Label(c, i, "data from resultset");
sheet.addCell(label);
}
i++;
}
completed += (end - completed);
start = end;
end = (totalsize - completed) < maxSheetSize ? (totalsize - completed) : completed + maxSheetSize;
end = end < maxSheetSize ? totalsize : end;
}
workbook.write();
workbook.close();