如何在ASP MVC中下载NPOI生成的xls文件 [英] how to download a xls file generated by NPOI in ASP MVC
本文介绍了如何在ASP MVC中下载NPOI生成的xls文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了一个示例,说明如何将Excel电子表格流式传输回客户端,但使用aspx代码.代码在下面
i found an exemple of how to Stream the Excel spreadsheet back to the client but in aspx code. the code is below
using (var exportData = new MemoryStream())
{
workbook.Write(exportData);
string saveAsFileName = string.Format("MembershipExport-{0:d}.xls", DateTime.Now).Replace("/", "-");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
我正在使用ASP MVC 5和webApi Controller.我想将此代码迁移到返回HttpResponseMessage的WebApiController.有什么建议吗?
i'am working with asp MVC 5 and webApi Controller. I want to migrate this code to WebApiController that returns HttpResponseMessage. Any propositions please ?
推荐答案
该帮助吗?
public FileResult DownloadFile()
{
// code to create workbook
using (var exportData = new MemoryStream())
{
workbook.Write(exportData);
string saveAsFileName = string.Format("MembershipExport-{0:d}.xls", DateTime.Now).Replace("/", "-");
byte[] bytes = exportData.ToArray();
return File(bytes, "application/vnd.ms-excel", saveAsFileName);
}
}
这篇关于如何在ASP MVC中下载NPOI生成的xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文