Skip to content

Instantly share code, notes, and snippets.

@erikwj
Created September 3, 2015 07:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erikwj/733ef4129d3d09f6f43a to your computer and use it in GitHub Desktop.
Save erikwj/733ef4129d3d09f6f43a 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