title: Corrupt Excel/spreadsheet file on export .Net Core Open XML
body: <h1>Corrupt Excel/spreadsheet file on export .Net Core Open XML</h1>
  <p>The exported spreadsheet file from the below code seems to be corrupted. I am using a .Net Core API to return the file with the following test code. Can anybody tell me what am I doing wrong?</p>
  <p>Thanks in advance!</p>
  <pre><code>[Route(&quot;api/tools&quot;)]
  public class ImportExportController : Controller
  {
      private readonly IHostingEnvironment _hostingEnvironment;
  
      public ImportExportController(IHostingEnvironment hostingEnvironment)
      {
          this._hostingEnvironment = hostingEnvironment;
      }
      [HttpGet]
      [Route(&quot;export&quot;)]
      public async Task&lt;IActionResult&gt; Export()
      {
          string sWebRootFolder = this._hostingEnvironment.WebRootPath;
          string sFileName = @&quot;demo.xls&quot;;
          string URL = string.Format(&quot;{0}://{1}/{2}&quot;, this.Request.Scheme, this.Request.Host, sFileName);
          FileInfo filePath = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
          var memory = new MemoryStream();
  
          SpreadsheetManager.SpreadsheetGenerator(filePath.FullName, sFileName);
  
          using (var stream = new FileStream(Path.Combine(sWebRootFolder, sFileName), FileMode.Open))
          {
              await stream.CopyToAsync(memory);
          }
  
          memory.Position = 0;
  
          return this.File(memory, &quot;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;, sFileName);
      }
  }
  </code></pre>
  <h2>This is the code for the SpreadsheetManager</h2>
  <pre><code>public static class SpreadsheetManager
  {
      public static void SpreadsheetGenerator(string filePath, string fileName)
      {
          if (File.Exists(filePath))
          {
              File.Delete(filePath);
          }
  
          SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook);
  
          using (document)
          {
             WorkbookPart workbookPart = document.AddWorkbookPart();
              workbookPart.Workbook = new Workbook();
  
              WorksheetPart worksheetPart = workbookPart.AddNewPart&lt;WorksheetPart&gt;();
              worksheetPart.Worksheet = new Worksheet(new SheetData());
  
              Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
  
              Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = &quot;Test Sheet&quot; };
  
              sheets.Append(sheet);
  
              workbookPart.Workbook.Save();
              
          }
      }
  }
  </code></pre>