title: Need help creating an Excel spreadsheet using NPOI from .Net Framework 4.8/Webform
body: <p>Sorry for long post but it's my first and I'm not know for my Goldilocks detail level, it's either too much or too little.</p>
  <p>I'm working on an internal site using .Net Framework 4.8: Webforms and attempting to create an Excel spreadsheet using data pulled from an Oracle view.</p>
  <p>I'm using NPOI (<a href="https://github.com/nissl-lab/npoi" rel="nofollow noreferrer">https://github.com/nissl-lab/npoi</a>) to try and accomplish this export.  I was able to use this same library to import spreadsheets into Oracle, so hoped I would have the same success exporting.  So far, no joy.</p>
  <p>Here's what's happening right now...</p>
  <p>My method successfully pulls the records I need, NPOI creates the worksheet, header row, cells, and data is populated in the worksheet object - I can see the data in the debugger.  I then attempt to load the worksheet into a MemoryStream and then attempt to return the MemoryStream as a spreadsheet attachment.  My expectation is that I'd see the browser's (Edge) Save As dialog appear letting me save the spreadsheet.  Instead, the method ends and nothing - no dialog, no browser error, no error in the debugger.</p>
  <p>This is the result of three days trying to complete this task.  Google has taken me to quite a few sites - so many that I really don't remember all the steps I've tried these past few days.  Seems most sites Google suggested were for .Net Core and even more that were for MVC.  Naturally I've search SO, but no joy here either.  I've hit enough of a wall that I've finally signed up to SO.</p>
  <p>Here's my method.  You can probably guess, I have an asp:Button that calls this method that's part of the page's code-behind:</p>
  <pre><code>protected void ExportHRAppToExcel_btn_Click(object sender, EventArgs e)
  {
  
      try
      {
          string sql = @&quot;select id, name, desc from list_select&quot;;
  
          ConnectionHrTrack conn = new ConnectionHrTrack();
          OracleDataAdapter da = new OracleDataAdapter(sql, conn.Open());
  
          DataTable dt = new DataTable();
  
          da.Fill(dt);
  
          da.Dispose();
          conn.Close();
          
          // filling in the workbook was adapted from
          // https://www.c-sharpcorner.com/blogs/export-to-excel-using-npoi-dll-library
  
          var properties = new[] { &quot;id&quot;, &quot;name&quot;, &quot;desc&quot; };
          var headers = new[] { &quot;ID&quot;, &quot;Name&quot;, &quot;Application Description&quot; };
  
          IWorkbook workbook;
          workbook = new XSSFWorkbook();
          ISheet sheet = workbook.CreateSheet(&quot;APPS&quot;);
  
          // create/fill header row  
          IRow row1 = sheet.CreateRow(0);
  
          for (int j = 0; j &lt; dt.Columns.Count; j++)
          {
              ICell cell = row1.CreateCell(j);
              String columnName = dt.Columns[j].ToString();
              cell.SetCellValue(columnName);
          }
  
          // create/fill data rows  
          for (int i = 0; i &lt; dt.Rows.Count; i++)
          {
              IRow row = sheet.CreateRow(i + 1);
              for (int j = 0; j &lt; dt.Columns.Count; j++)
              {
  
                  ICell cell = row.CreateCell(j);
                  String columnName = dt.Columns[j].ToString();
                  cell.SetCellValue(dt.Rows[i][columnName].ToString());
              }
          }
  
          using (MemoryStream exportData = new MemoryStream())
          {
              Response.Clear();
              workbook.Write(exportData);
              
              Response.ContentType = &quot;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;;
              Response.AddHeader(&quot;Content-Disposition&quot;, string.Format(&quot;attachment;filename={0}&quot;, &quot;hr_apps.xlsx&quot;));
              
              Response.BinaryWrite(exportData.ToArray());
  
              // in lieu of Response.End(), which is said to kill the memorystream and did return exceptions,
              // used the following.  Found the same three lines in different forums, one of which was:
              // https://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce
              Response.Flush(); // Sends all currently buffered output to the client.
              Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
              HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
          }
  
      }
      catch (Exception ex)
      {
          // here so i can see the exception when debugging in VS since the above returns *nothing* to the browser
          Console.WriteLine(ex.Message);
      }
  }
  </code></pre>
  <p>I've seen enough posts to know that NPOI will create a spreadsheet that's downloadable.  So, obviously the error is on my side, but I haven't been able to find it.  And maybe the code in my method is fine but the way I'm trying to consume it is not.  Alas, I've not seen any examples of how methods are used, just the samples of code within.</p>
  <p>Any suggestions would be greatly appreciated - especially if they help me get past this wall of frustration.</p>
  <p>EDIT: I forgot to mention, Chrome reports the following at the conclusion of the operation:</p>
  <p><em>Resource interpreted as Document but transferred with MIME type application/octet-stream: &quot;{localhost URL/page}&quot;.</em></p>
  <p>None of my Google searches resulted anything close to useful.</p>
  <p>** RESOLVED **</p>
  <p>Apparently the files <em>have been</em> created.  For some reason, Edge never notified me to ask if I wanted to save/open.  Instead, it had been saving the files in MicrosoftEdgeDownloads folder in AppData Local and doing so quietly.  I'm not sure why I didn't get the notification since I have it set to notify me.</p>
  <p>In my defense, I rarely use Edge.</p>