Write files based on the SXSSF (streaming Usermodel API)

Source: Internet
Author: User

In POI3.8, SXSSF only supports excel2007 format as an extension of a stream of XSSF. The purpose of this is to provide the efficiency of writing data by refreshing the Excel memory information to the hard disk when generating a large amount of data in the form of Excel.

The official text reads as follows:

SXSSF (streaming Usermodel API) Note SXSSF is a brand new contribution and some features were added after it was F Irst introduced in POI 3.8-beta3. Users is advised to try the latest build from trunk. Instructions how to build is here.

SXSSF (package:org.apache.poi.xssf.streaming) is a api-compatible streaming extension of XSSF to being used when very l Arge spreadsheets are produced, and heap space is limite d. SXSSF achieves it low memory footprint by limiting Access to the rows is within a sliding window, while XSSF gives access to all rows in the document. Older rows that is no longer in the window become inaccessible, as they is written to the disk.

You can specify the window size @ Workbook construction time via new Sxssfworkbook (int windowsize) or you can SE T it per-sheet via sxssfsheet#setrandomaccesswindowsize (int windowsize)

When a new row was created via CreateRow () and the total number of unflushed records would exceed the specified window Size, then the row with the lowest index value are flushed a nd cannot be accessed via GetRow () anymore.

The default window size is A and defined by Sxssfworkbook.default_window_size.

A windowsize of-1 indicates unlimited access. In the This is records that has not been flushed by a call to Flushrows () is available for random access.

The example below writes a sheet with a window of $ rows. When the row was Count reaches 101, the row with rownum=0 was flushed to disk and removed from memory, when RowNum reaches 102 Then the row with Rownum=1 is flushed, etc.

The test code is as follows:

Java code
  1. Package com.easyway.excel.events.stream;
  2. Import Java.io.FileOutputStream;
  3. Import Org.apache.poi.ss.usermodel.Cell;
  4. Import Org.apache.poi.ss.usermodel.Row;
  5. Import Org.apache.poi.ss.usermodel.Sheet;
  6. Import Org.apache.poi.ss.usermodel.Workbook;
  7. Import org.apache.poi.ss.util.CellReference;
  8. Import Org.apache.poi.xssf.streaming.SXSSFWorkbook;
  9. /**
  10. * SXSSF (streaming Usermodel API)
  11. * When the file is written in a particularly large stream, flush the in-memory data to the hard disk, reducing the amount of memory used.
  12. * Play the role of space for time, to provide efficiency.
  13. *
  14. * @Title:
  15. * @version 1.0
  16. */
  17. Public class Sxssexcelevent {
  18. public static void Main (string[] args) throws Throwable {
  19. //Create a stream-based workbook object
  20. Workbook WB = new Sxssfworkbook (100); //Keep rows in memory, exceeding rows would be flushed to disk
  21. //sxssfworkbook wb = new Sxssfworkbook ();
  22. //wb.setcompresstempfiles (TRUE);//Temp files would be gzipped
  23. Sheet sh = wb.createsheet ();
  24. //Use CreateRow to write information in memory.
  25. For (int rownum = 0; rownum < ; rownum++) {
  26. Row row = Sh.createrow (rownum);
  27. For (int cellnum = 0; cellnum < cellnum++) {
  28. Cell cell = Row.createcell (Cellnum);
  29. String address = new cellreference (cell). formatasstring ();
  30. Cell.setcellvalue (address);
  31. }
  32. }
  33. //Rows with rownum < is flushed and not accessible
  34. //When accessing using the GetRow method, flush the in-memory information to the hard disk.
  35. For (int rownum = 0; rownum < ; rownum++) {
  36. System.out.println (Sh.getrow (rownum));
  37. }
  38. //ther last rows is still in memory
  39. For (int rownum = N, rownum < rownum++) {
  40. System.out.println (Sh.getrow (rownum));
  41. }
  42. //write in file
  43. FileOutputStream out = new FileOutputStream ("c://sxssf.xlsx");
  44. Wb.write (out);
  45. //Close File stream object
  46. Out.close ();
  47. System.out.println ("Based on stream write execution!");
  48. }
  49. }

SXSSF Flushes sheet data in temporary files (a temp file per sheet) and the size of these temporary files can grow to A very large value. For example, A-MB CSV data, the size of the temp XML becomes more than a gigabyte. If the size of the temp files is a issue, you can tell SXSSF to use gzip compression:

  Sxssfworkbook wb = new Sxssfworkbook ();  Wb.setcompresstempfiles (TRUE);//Temp files would be gzipped

Write files based on the SXSSF (streaming Usermodel API)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.