title: JAVA Apache POI and large result set from DB2 runs into exception
body: <p>I try to create from a result set an XLSX file with Apache POI. The data comes from a local DB2 instance. The table I am querying has 580,000 entries.</p>
  
  <p>Everything goes well, if I query for example the first 15k of rows, but when I try to query for all rows I get the following exception and the file is corrupted when I try to open it in Excel.</p>
  
  <p>I'm using Apache POI 4.0.0 and Java 1.8</p>
  
  <pre><code>Exception in thread "main" java.io.IOException: This archive contains unclosed entries.
  at org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.finish(ZipArchiveOutputStream.java:467)
  at org.apache.poi.xssf.streaming.SXSSFWorkbook.injectData(SXSSFWorkbook.java:406)
  at org.apache.poi.xssf.streaming.SXSSFWorkbook.write(SXSSFWorkbook.java:936)
  at exportexcel.ExportExcel.createXLSX(ExportExcel.java:110)
  at exportexcel.ExportExcel.main(ExportExcel.java:137)
  C:\Users\USER\AppData\Local\NetBeans\Cache\9.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
  C:\Users\USER\AppData\Local\NetBeans\Cache\9.0\executor-snippets\run.xml:94: Java returned: 1
  BUILD FAILED (total time: 12 minutes 19 seconds)
  </code></pre>
  
  <p>The line the exception points to is <code>workbook.write(out);</code></p>
  
  <p>Below my code I'm using</p>
  
  <pre><code>package exportexcel;
  
  import java.io.File;
  
  import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  import org.apache.poi.xssf.streaming.SXSSFCell;
  import org.apache.poi.xssf.streaming.SXSSFRow;
  import org.apache.poi.xssf.streaming.SXSSFSheet;
  
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  
  public class ExportExcel {
  
  private static final String FILE_NAME = "c://file.xlsx";
  
  public static void createXLSX() throws FileNotFoundException, IOException
  {
      SXSSFWorkbook workbook = new SXSSFWorkbook(100);
      workbook.setCompressTempFiles(true);
      SXSSFSheet sheet = workbook.createSheet("RawData");
  
      String url = "jdbc:db2://localhost:50000/local";
      java.util.Properties properties = new java.util.Properties();
      properties.put("user", "user");
      properties.put("password", "pass");
      Connection con;
  
      Statement stmt = null;
      ResultSet rs;
  
      try {
          // Load the driver
          Class.forName("com.ibm.db2.jcc.DB2Driver");
          System.out.println("**** Loaded the JDBC driver\n");
  
          // Create the connection using the IBM Data Server Driver for JDBC and SQLJ
          con = DriverManager.getConnection (url, properties);
          // Commit changes manually
          con.setAutoCommit(false);
          System.out.println("**** Created a JDBC connection to the data source\n");
  
  
          stmt = con.createStatement();
          rs = stmt.executeQuery("SELECT STATEMENT");
  
          System.out.println("**** Created JDBC ResultSet object\n\n");
  
          ResultSetMetaData rsMetaData = rs.getMetaData();
  
          int numberOfColumns = rsMetaData.getColumnCount();
  
          SXSSFRow row = sheet.createRow(0);
          SXSSFCell cell;
  
          for (int i = 1; i &lt;= numberOfColumns; i++)
          {
              cell = row.createCell(i-1);
              cell.setCellValue(rsMetaData.getColumnName(i));
          }
  
          int i = 1;
  
              while (rs.next()) {
                 row = sheet.createRow(i);
                  for (int j = 1; j &lt;= numberOfColumns; j++)
                 {
                     cell = row.createCell(j-1);
                     cell.setCellValue(rs.getString(j));
                 }
              i++;
              System.out.println(i);
              }            // Execute a query and generate a ResultSet instance
  
          System.out.println("\n**** Fetched all rows from JDBC ResultSet\n");
          // Close the ResultSet
          rs.close();
          System.out.println("**** Closed JDBC ResultSet\n");
  
          // Close the Statement
          stmt.close();
          System.out.println("**** Closed JDBC Statement\n");
  
          // Connection must be on a unit-of-work boundary to allow close
          con.commit();
          System.out.println("**** Transaction committed\n");
  
          // Close the connection
          con.close();
          System.out.println("**** Disconnected from data source\n");
  
          try (FileOutputStream out = new FileOutputStream(new File(FILE_NAME))) {
              workbook.write(out);
              workbook.dispose();
              workbook.close();
              out.flush();
          }
          System.out.println("File written!");
      }
  
      catch (ClassNotFoundException e)
      {
          System.err.println("Could not load JDBC driver");
          System.out.println("Exception: " + e);
      }
  
      catch(SQLException ex)
      {
          System.err.println("SQLException information");
          while(ex!=null) {
              System.err.println ("Error msg: " + ex.getMessage());
              System.err.println ("SQLSTATE: " + ex.getSQLState());
              System.err.println ("Error code: " + ex.getErrorCode());
              ex = ex.getNextException(); // For drivers that support chained exceptions
       }        
  }
  }
  
  public static void main(String[] args) throws IOException {
      createXLSX();
  }
  }
  </code></pre>
  
  <p>I would really appreciate if someone could help.</p>
  
  <p>Thank you in advance</p>