How Java uses POI to export Excel with large amounts of data
This article mainly shows you "how Java uses POI to export large amounts of data Excel". The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how Java uses POI to export large amounts of data Excel".
profile
SXSSFWorkbook requires poi-ooxml package 3.8 and above to start supporting, I am using version 3.9, essentially an XSSFWorkbook class (Excel 2007), it uses the hard disk space to greatly reduce the heap memory occupation, in the system temporary folder directory to create a temporary file, and then all the data larger than the agreed number of rows are stored in the temporary file, rather than all in memory, memory only stores the latest agreed number of data, so as to achieve hard disk space in exchange for memory space, to avoid memory overflow
use
There is no difference from the normal Excel export method, just replace the instantiated class with SXSSFWorkbook
SXSSFWorkbook workbook = null; OutputStream outputStream = null; try { outputStream = response.getOutputStream(); //Create workbook workbook = new SXSSFWorkbook(); //Turn on compression to prevent excessive disk usage workbook. setCompressed TempFiles (true); //Create a worksheet Sheet = workbook.createSheet("Table Name"); //Create a Row titleRow = sheet.createRow(0);//Create a cell Cell = titleRow.createCell(0); //Assign cell.setCellValue("Content"); //Write workbook to output stream workbook.write(outputStream); } catch (Exception e) { e.printStackTrace(); }finally { if (workbook != null) { //delete temporary files generated after use to prevent disk from filling workbook.dispose(); } if (outputStream != null) { outputStream.close(); } }
The above is "Java how to use POI export large amounts of data Excel" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!