issue with returning HttpResponseMessage as excel file in WebAPI
<p>I have created WebAPI which returns an excel file using closedxml nuget. Basically it converts my <code>DataTable</code> to  excel. I'm referring couple of links below,</p>

<ol>
<li><p><a href="https://stackoverflow.com/questions/26038856/how-to-return-a-file-filecontentresult-in-asp-net-webapi">How to return a file (FileContentResult) in ASP.NET WebAPI</a></p></li>
<li><p><a href="https://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api?rq=1">Returning binary file from controller in ASP.NET Web API</a></p></li>
</ol>

<p><strong>Issue :</strong> excel generated on server path has NO issues. But when I download the same by returning it as <code>HttpResponseMessage</code> via webAPI the excel file is corrupted. It says, "The file is corrupt and cannot be opened" :(</p>

<p><a href="https://i.stack.imgur.com/t7BEy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t7BEy.png" alt="enter image description here"></a></p>

<p><strong>My Code :</strong> </p>

<pre><code>    [System.Web.Http.AcceptVerbs("GET", "POST")]
    public HttpResponseMessage ExportExcel()
            {                 
                     DataTable scoredRecords = Getdt();
                     if (scoredRecords.Rows.Count &gt; 0)
                        {
                            var path = @"C:\Raghav\asdf.xlsx";
                            XLWorkbook wb = new XLWorkbook();
                            wb.Worksheets.Add(scoredRecords, "sample");
                            // excel getting generated on server properly-No issues.
                            wb.SaveAs(path);
                            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                            var stream = new FileStream(path, FileMode.Open);
                            result.Content = new StreamContent(stream);
                            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                            {
                                FileName = "sample.xlsx"
                            };
                            result.Content.Headers.ContentLength = stream.Length;
                            //tried with "application/ms-excel" also
                            result.Content.Headers.ContentType =
                                new MediaTypeHeaderValue("application/octet-stream");
                            return result;
                        }

               }
</code></pre>

<p><strong>The generated excel on server has no issues.</strong> Only the downloaded excel file via webAPI is corrupted. Unable to figure out the issue.. 
any help appreciated!! :)</p>