这是我的代码..
[HttpPost]
public ActionResult Export()
{
GridView grid = new GridView();
StringWriter sw = new StringWriter();
var ItemsFromDb = lst.FetchDataFromList(Session["userId"].ToString()).ToList();
if (ItemsFromDb.Count != 0)
{
grid.DataSource = ItemsFromDb;
grid.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("Content-disposition", "Attachment;filename=" + "Members_" + DateTime.Now.ToString("dd/mm/yyy") + ".pdf");
Response.ContentType = "application/pdf";
Response.Charset = "";
HtmlTextWriter hw = new HtmlTextWriter(sw);
grid.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
else
{
TempData["Message"] = "Sorry.. No data to export.";
return View();
}
return RedirectToAction("UserDetails","EmployeeLocation");
}
此代码仅适用于创建和下载Excel格式,仅将类型更改为.xls
和"application/ms-excel"
但是数据没有加载的是pdf ...