1
public
static
void
ExportGridView(GridView gridView,
string
filename)
2![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
string attachment = "attachment; filename=" + filename + ".xls";
4
HttpResponse Response = HttpContext.Current.Response;
5
Response.ClearContent();
6
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
7
Response.AddHeader("content-disposition", attachment);
8
Response.ContentType = "application/ms-excel";
9
StringWriter sw = new StringWriter();
10
HtmlTextWriter htw = new HtmlTextWriter(sw);
11
// Create a form to contain the grid
12
HtmlForm frm = new HtmlForm();
13
gridView.Parent.Controls.Add(frm);
14
frm.Attributes["runat"] = "server";
15
frm.Controls.Add(gridView);
16
frm.RenderControl(htw);
17
//GridView1.RenderControl(htw);
18
Response.Write(sw.ToString());
19
Response.End();
20
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
15
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)