我使用以下代码导出excel。 htmlData是在客户端生成的html数据。我在Api控制器中编写了这个方法。
public HttpResponseMessage ExportXls(string htmlData)
{
try
{
byte[] excelData = Encoding.ASCII.GetBytes(htmlData.Trim());
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(excelData);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Data.xls"
};
return result;
}
}
我在打开文件时收到xls文件格式的警告。有没有解决办法?
我还想询问是否有任何方法可以导出excel而不会收到任何警告但不使用任何第三方dll如eppplus和closedXML。
是否有人能够通过仅使用响应来生成excel而不发出警告?
答案 0 :(得分:0)
您想要指定它看起来正确的mime类型。
即application / vnd.ms-excel
这可能也很有用, What is a correct mime type for docx, pptx etc?
值得关注
这可能会帮到你。
答案 1 :(得分:0)
我将csv格式的html数据发送到服务器。使用拆分函数将它们添加到closedXml单元格,并能够以xlsx格式成功生成excel。