【发布时间】:2016-12-08 09:08:57
【问题描述】:
public ActionResult ExportToExcel()
{
GridView gv = new GridView();
gv.DataSource = db.Details.Select(x => new
{
x.Title,
Category = x.Category.Title,
SubCategory = x.SubCategory.Title,
x.Count,
x.Price,
Automobiles = x.Automobiles.Count,
x.Description
}).ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Marklist.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return Json(true);
}
Export 在本地运行良好。它不适用于 Azure。下载一个空文件。告诉我有什么问题?
【问题讨论】:
-
这是在使用 COM 吗?
-
还需要安装 Excel 才能在本地工作吗?
-
@evilSnobu 不,这显然不使用 COM 或 Excel 互操作。
-
你在服务器上调试过吗?确认您实际上已从数据库中检索到数据?为什么要生成 HTML 文件并使用 XLS 扩展名提供它们?这是一个糟糕的做法,微软已经发布了一个更新,这几乎无法接受。为什么要从方法返回 JSON 并结束响应而不是返回文件结果?为什么在 ASP.NET MVC 中使用 GridView?
标签: c# asp.net-mvc azure xls