title: C# Not sure how to handle System.Web.HttpResponse
body: <p>When I use this class on my local machine it prompts the user to open the xlsx file, but when I publish to Azure it no longer works.  I am pretty sure my problem it that I am not handling the filestream when on Azure.  Hopefully someone can help me out.</p>
  
  <p>The code that works when I publish locally -</p>
  
  <pre><code>if (vm.ExportClicked != null &amp;&amp; vm.ExportClicked.ToLower() == "true")
              {
                  //doing this because I need the entire list not just a single page
                  CustomerLastOrderListViewModel vm1 = new CustomerLastOrderListViewModel();
                  var oPagedCustomerLists = oFilteredCustomerList.OrderByDescending(c =&gt; c.Id);
  
                  //.ToList();
                  List&lt;CustomerLastOrderViewModel&gt; customersLastOrders = CombineWithLastOrder(oPagedCustomerLists.ToList());
                  vm1.CustomersLastOrder = customersLastOrders;
                  DataTable dt = CreateCustomerEmailData(vm1, form);
                  CreateExcelFile.CreateExcelDocument(dt, "CustomerEmailList.xlsx", HttpContext.ApplicationInstance.Response);
  </code></pre>
  
  <p>I am using this response because I could not get it to compile with anything else.  The Excel class returns the information in a filestream and I am wondering since I am on Azure and Excel is not installed there I have to do something differently.</p>
  
  <p>This is the CreateExcelDocument -</p>
  
  <pre><code>    public static bool CreateExcelDocument(DataTable dt, string filename, System.Web.HttpResponse Response)
      {
          try
          {
              DataSet ds = new DataSet();
              ds.Tables.Add(dt);
              CreateExcelDocumentAsStream(ds, filename, Response);
              ds.Tables.Remove(dt);
              return true;
          }
          catch (Exception ex)
          {
              Trace.WriteLine("Failed, exception thrown: " + ex.Message);
              return false;
          }
      }
  
  public static bool CreateExcelDocumentAsStream(DataSet ds, string filename, System.Web.HttpResponse Response)
  {
      try
      {
          System.IO.MemoryStream stream = new System.IO.MemoryStream();
          using (SpreadsheetDocument document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
          {
              WriteExcelFile(ds, document);
          }
          stream.Flush();
          stream.Position = 0;
  
          Response.ClearContent();
          Response.Clear();
          Response.Buffer = true;
          Response.Charset = "";
  
          //  NOTE: If you get an "HttpCacheability does not exist" error on the following line, make sure you have
          //  manually added System.Web to this project's References.
  
          Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
          Response.AddHeader("content-disposition", "attachment; filename=" + filename);
          Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
          byte[] data1 = new byte[stream.Length];
          stream.Read(data1, 0, data1.Length);
          stream.Close();
          Response.BinaryWrite(data1);
          Response.Flush();
          Response.End();
  
          return true;
      }
      catch (Exception ex)
      {
          Trace.WriteLine("Failed, exception thrown: " + ex.Message);
          return false;
      }
  }
  </code></pre>