title: Download EXCEL file from ASP.NET page without Generating physical file on server (On The Fly)
body: <p>I am trying to export C# DataTable to EXCEL file on the fly (without creating physical file) using Microsoft Office EXCEL INTEROP and download it through asp.net webpage through Response object. I am able to generate memorystream using the library But somehow the file is not getting saved through the memory stream. Reference code is given below. You will be required to take reference for DocumentFormat.OpenXml.dll &amp; WindowsBase.DLL (that you can download from microsoft site).</p>
  
  <p>Any Idea how to resolve the problem ? ? ?</p>
  
  <pre><code>Private void DownloadFile()
  {
                  DataSet objTable = ReadTableFromViewstate();
              if (objTable != null &amp;&amp; objTable.Rows.Count &gt; 0)
              {
  
                  string strDownloadableFilename = "TestExcelFileName.xls";
                  MemoryStream fs1 = new MemoryStream();
                  if (CreateExcelFile.CreateExcelDocument(objTable, fs1))
                  {
                      Response.Clear();
                          byte[] data1 = new byte[fs1.Length];
                          fs1.Read(data1, 0, data1.Length);
                          fs1.Close();
  
                      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                      Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", strDownloadableFilename));
                      Response.BinaryWrite(data1); ;
                     Response.End();
  
                  }
                  else
                  {
                      LblErrorMessage.Text = "Error Exporting File.";
                  }
              }
  
          }
  </code></pre>
  
  <p>..</p>
  
  <pre><code>public static bool CreateExcelDocument(DataSet ds, System.IO.Stream excelFileStream)
      {
          try
          {
              using (SpreadsheetDocument document = SpreadsheetDocument.Create(excelFileStream, SpreadsheetDocumentType.Workbook))
              {
                  CreateParts(ds, document);
              }
  
              Trace.WriteLine("Successfully created: " + excelFileStream);
              return true;
          }
          catch (Exception ex)
          {
              Trace.WriteLine("Failed, exception thrown: " + ex.Message);
              return false;
          }
      }
  ..
  
  
  
  
  private static void CreateParts(DataSet ds, SpreadsheetDocument document)
          {
              WorkbookPart workbookPart = document.AddWorkbookPart();
              Workbook workbook = new Workbook();
              workbookPart.Workbook = workbook;
  
              //  If we don't add a "WorkbookStylesPart", OLEDB will refuse to connect to this .xlsx file !
  
                  WorkbookStylesPart workbookStylesPart = workbookPart.AddNewPart&lt;WorkbookStylesPart&gt;("rIdStyles");
              Stylesheet stylesheet = new Stylesheet();
  
              workbookStylesPart.Stylesheet = stylesheet;
  
              Sheets sheets = new Sheets();
  
              //  Loop through each of the DataTables in our DataSet, and create a new Excel Worksheet for each.
          uint worksheetNumber = 1;
          foreach (DataTable dt in ds.Tables)
          {
              //  For each worksheet you want to create
              string workSheetID = "rId" + worksheetNumber.ToString();
              string worksheetName = dt.TableName;
  
              WorksheetPart worksheetPart = workbookPart.AddNewPart&lt;WorksheetPart&gt;(workSheetID);
              WriteDataTableToExcelWorksheet(dt, worksheetPart);
  
              Sheet sheet = new Sheet() { Name = worksheetName, SheetId = (UInt32Value)worksheetNumber, Id = workSheetID };
              sheets.Append(sheet);
  
              worksheetNumber++;
          }
  
          workbook.Append(sheets);
      }
  </code></pre>