0

I'm using WebApiContrib.Formatting.xlsx to create and download a Microsoft Excel file. My problem is that when called from the client the file can't be opened and several method were tried. When dealing with PDF the problem was resolved converting the output to Byte64 like this:

var response = new HttpResponseMessage
                {
                    Content = new StringContent(Convert.ToBase64String(output.ToArray())) 
                };
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "Report.pdf"
                };

            return response;

In the case of WebApiContrib.Formatting.xlsx I have an IEnumerable. How can convert it the same way as the PDF.

var subscriptionExcelModels = subscriptionPlansConverted as SubscriptionExcelModel[] ?? subscriptionPlansConverted.ToArray();

This is my snippet of the last part of the code for the excel file:

   var ret = Request.CreateResponse(HttpStatusCode.OK,subscriptionExcelModels); 
    ret.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    ret.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "Report.xlsx"
    };

    return ret;
polonskyg
  • 4,269
  • 9
  • 41
  • 93

0 Answers0