Skip to content

Instantly share code, notes, and snippets.

@vnt-83
Forked from erikwj/StreamingExcel.scala
Created February 16, 2023 02:54
Show Gist options
  • Save vnt-83/733d9e14d98f6bce9d6bf1b549c7f265 to your computer and use it in GitHub Desktop.
Save vnt-83/733d9e14d98f6bce9d6bf1b549c7f265 to your computer and use it in GitHub Desktop.
create large excel files
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.FileOutputStream
import java.io.File
object StreamingExcel {
def main(args: Array[String]): Unit = {
val wb = new SXSSFWorkbook(10); // keep 10 rows in memory, exceeding rows will be flushed to disk
val sh = wb.createSheet();
for (rownum ← 0 to 50000) {
val row = sh.createRow(rownum);
for (cellnum ← 0 to 60) {
val cell = row.createCell(cellnum);
val address = new CellReference(cell).formatAsString();
cell.setCellValue(address);
}
}
val out = new FileOutputStream(new File("~/data/sxssf.xlsx"));
wb.write(out);
out.close();
// dispose of temporary files backing this workbook on disk
wb.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment