62,241
社区成员




public ActionResult ExportToExcel()
02
{
03
var products = _productRepository.GetAll();
04
05
var grid = new GridView();
06
grid.DataSource = from p in products
07
select new
08
{
09
ProductName = p.ProductName,
10
SomeProductId = p.ProductID
11
};
12
grid.DataBind();
13
14
Response.ClearContent();
15
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
16
17
Response.ContentType = "application/excel";
18
19
StringWriter sw = new StringWriter();
20
21
HtmlTextWriter htw = new HtmlTextWriter(sw);
22
23
grid.RenderControl(htw);
24
25
Response.Write(sw.ToString());
26
27
Response.End();
28
29
return View("Index");
30
}