我正在使用WebApiContrib.Formatting.xlsx来创建和下载Microsoft Excel文件。我的问题是,当从客户端调用时,无法打开文件并尝试了几种方法。在处理PDF时问题得到解决,将输出转换为Byte64,如下所示:
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;
对于WebApiContrib.Formatting.xlsx,我有一个IEnumerable。如何将其转换为与PDF相同的方式。
var subscriptionExcelModels = subscriptionPlansConverted as SubscriptionExcelModel [] ?? subscriptionPlansConverted.ToArray();
这是excel文件代码的最后一部分的片段:
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;