导出大量数据到 excel 的 xlsx文件
static String src="abcdefafslfelgtryjukjhgfdadertjDSFGHJKJGHFERTUIOabcdefafslfelgtryjukjhgfdadertjDSFGHJKdertjDSFGHJKJGHFERTUIOabcdefafslfelgtryjukjhgfdadertjDSFGHJKJGHFERTUIO";
public static void main(String[] args) throws Throwable {
SXSSFWorkbook wb = new SXSSFWorkbook(100); // 这里100是在内存中的数量,如果大于此数量时,会写到硬盘,以避免在内存导致内存溢出
Sheet sh = wb.createSheet();
for (int rownum = 0; rownum < 1000000; 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+src.substring(rownum%10*10+1, (rownum%10+1)*10));
}
}
File file = new File("F:/aa.xlsx");
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
}
内存使用情况: