asp.net MVC项目中 如何在Action内使用NPOI导出Excel?
public ActionResult ExportExcel(FormCollection form)
{
HttpContext cur = System.Web.HttpContext.Current;
string filename = "test.xls";
cur.Response.ContentType = "application/vnd.ms-excel";
cur.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
cur.Response.Clear();
InitializeWorkbook(); //初始化工作簿
GenerateData(); //给Excel生成数据
cur.Response.BinaryWrite(WriteToStream().GetBuffer());
cur.Response.End();
return View();
}
在页面中 是 通过单击一个按钮(导出),调用对应Controller的Action(ExportExcel)
出错信息:服务器无法在已发送 HTTP 标头之后设置状态。不明白,我是按照 NPOI demo的例子 模仿的,例子 是可以导出的,我的就不行了。难道 这个跟 项目 是MVC框架有关吗?