title: Error While Reading Large Excel Files (xlsx) Via Apache POI
body: <p>I am trying to read large excel files xlsx via Apache POI, say 40-50 MB. I am getting out of memory exception. The current heap memory is 3GB.</p>
  
  <p>I can read smaller excel files without any issues. I need a way to read large excel files and then them back as response via Spring excel view.</p>
  
  <pre><code>public class FetchExcel extends AbstractView {
  
  
      @Override
      protected void renderMergedOutputModel(
              Map model, HttpServletRequest request, HttpServletResponse response) 
      throws Exception {
  
      String fileName = "SomeExcel.xlsx";
  
      response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  
      OPCPackage pkg = OPCPackage.open("/someDir/SomeExcel.xlsx");
  
      XSSFWorkbook workbook = new XSSFWorkbook(pkg);
  
      ServletOutputStream respOut = response.getOutputStream();
  
      pkg.close();
      workbook.write(respOut);
      respOut.flush();
  
      workbook = null;                    
  
      response.setHeader("Content-disposition", "attachment;filename=\"" +fileName+ "\"");
  
  
      }    
  
  }
  </code></pre>
  
  <p>I first started off using <code>XSSFWorkbook workbook = new XSSFWorkbook(FileInputStream in);</code>
  but that was costly per Apache POI API, so I switched to OPC package way but still the same effect. I don't need to parse  or process the file, just read it and return it.</p>