在WebAPI中将HttpResponseMessage作为excel文件返回的问题

时间:2016-12-17 16:36:06

标签: c# excel asp.net-web-api closedxml

我创建了WebAPI,它使用closedxml nuget返回一个excel文件。基本上它将我的DataTable转换为excel。我在下面提到几个链接,

  1. How to return a file (FileContentResult) in ASP.NET WebAPI

  2. Returning binary file from controller in ASP.NET Web API

  3. 在服务器路径上生成的

    问题: excel没有问题。但是当我通过webAPI将其作为HttpResponseMessage返回时下载相同内容时,excel文件已损坏。它说,“文件已损坏,无法打开”:(

    enter image description here

    我的代码:

        [System.Web.Http.AcceptVerbs("GET", "POST")]
        public HttpResponseMessage ExportExcel()
                {                 
                         DataTable scoredRecords = Getdt();
                         if (scoredRecords.Rows.Count > 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;
                            }
    
                   }
    

    服务器上生成的excel没有问题。只有通过webAPI下载的excel文件已损坏。无法弄清楚问题.. 任何帮助赞赏!! :)

2 个答案:

答案 0 :(得分:1)

试试这个:

    [HttpGet]
    public HttpResponseMessage Export()
    {
        using (var wb = new XLWorkbook())
        using (MemoryStream ms = new MemoryStream())
        {
            wb.Worksheets.Add("sample").FirstCell().SetValue("some value");

            wb.SaveAs(ms);

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(ms.GetBuffer());
            result.Content.Headers.ContentLength = ms.Length;
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "sample.xlsx"
            };
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            return result;
        }
    }

答案 1 :(得分:0)

我看到的一个问题是:

 var stream = new FileStream(path, FileMode.Open);

您正在发送FileStream。除此之外,您可以尝试使用byte []

byte[] excelData  = File.ReadAllBytes(path);
result.Content = new StreamContent(excelData);

你可以尝试一下。