title: System.OutOfMemoryException while writing to excel
body: <pre><code>        Public Function GenerateReportAsExcel()
          Dim workbook = WorkbookFactory.Create(template)
          template.Close()
          Dim worksheet = workbook.GetSheetAt(0)
  
      //           Writing record to worksheet
  
  
          Dim workbookStream = New MemoryStream()
          workbookStream.Flush()
          workbookStream.Position = 0
          workbook.Write(workbookStream)  //throws error if the rocord is more then 500000...runs fine for 400000 
  
          Return New MemoryStream(workbookStream.ToArray())
      End Function
  </code></pre>
  
  <p>WorkbookFactory is using NPOI.SS.UserModel ....</p>
  
  <p>Is there a way to increase the memory stream capacity? I am getting  System.OutOfMemoryException  while writing 500000 record to the excel but upto  400000 record works fine.
  I found couple of similar issue but not getting any solid solution to this problem... 
  Someone one suggested  to use </p>
  
  <p>workbookStream.Flush()
  workbookStream.Position = 0
  but not of any help....</p>
  
  <p>Thanks for the concern..</p>