title: How to return a MemoryStream as an Excel file from IActionResult in Asp.Net Core
body: <p>I'm working on an <code>Asp.Net Core</code> project targeted <code>.Net 5</code>.</p>
  <p>I have this <code>ActionResult</code> :</p>
  <pre><code>     public async Task&lt;ActionResult&gt; ExportMarksForm(int testId)
              {
                  var stream = new MemoryStream();
                  // Some logic to Save the workbook as stream
  
                  return File(stream, &quot;application/ms-excel&quot;, &quot;xxx.xlsx&quot;);
              }
  </code></pre>
  <p><strong>The problem:</strong>
  Everything works fine but when I downloaded the file I got an excel message that told me that the file is damaged, that is means an error happened or the file writing operation not complete.</p>
  <p>I tried <strong>break points</strong> and I saw that the <code>stream</code> variable has <code>bytes</code>.</p>
  <p>So, please how can I fix this issue? or if there is any other solution to return a <code>stream</code> as an Excel file please share it with me.
  Thanks in advance.</p>