(一). 运行示例图
1. 待导出数据的GridView图:
![Asp.net 2.0 GridView数据导出Excel文件](http://img.e-com-net.com/image/product/9133549a011345279b8f7c1de7cf53cd.jpg)
2. 生成的Excel文件
![Asp.net 2.0 GridView数据导出Excel文件](http://img.e-com-net.com/image/product/2b71001352b54accbcd4540674875f65.jpg)
(二). 代码
1. 前台页面 GridViewToExcelFile.aspx 代码:
1
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<%@PageLanguage="C#"AutoEventWireup="true"
EnableEventValidation="false"CodeFile="GridViewToExcelFile.aspx.cs"Inherits="_Default"%>
2
3
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<htmlxmlns="http://www.w3.org/1999/xhtml">
6
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<headrunat="server">
7
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<title>UntitledPage</title>
8
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
</head>
9
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<body>
10
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<formid="form1"runat="server">
11
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<div>
12
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<asp:GridViewID="gvFoods"runat="server"BackColor="LightGoldenrodYellow"BorderColor="Tan"BorderWidth="1px"CellPadding="2"ForeColor="Black"GridLines="None"EnableViewState="true">
13
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<FooterStyleBackColor="Tan"/>
14
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<SelectedRowStyleBackColor="DarkSlateBlue"ForeColor="GhostWhite"/>
15
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<PagerStyleBackColor="PaleGoldenrod"ForeColor="DarkSlateBlue"HorizontalAlign="Center"/>
16
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<HeaderStyleBackColor="Tan"Font-Bold="True"/>
17
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<AlternatingRowStyleBackColor="PaleGoldenrod"/>
18
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
</asp:GridView>
19
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<br/>
20
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
<asp:ButtonID="Button1"runat="server"BackColor="#C0C0FF"OnClick="Button1_Click"
21
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
Text="CreateExcel"Width="137px"/>
22
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
</div>
23
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
</form>
24
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
</body>
25
![](http://img.e-com-net.com/image/product/46339e8aae704cb1a8b93e7cd90036c0.gif)
</html>
2. 页面后台文件 GridViewToExcelFile.aspx.cs 代码:
1usingSystem;
2usingSystem.Data;
3usingSystem.Configuration;
4usingSystem.Web;
5usingSystem.Web.Security;
6usingSystem.Web.UI;
7usingSystem.Web.UI.WebControls;
8usingSystem.Web.UI.WebControls.WebParts;
9usingSystem.Web.UI.HtmlControls;
10usingSystem.IO;
11usingSystem.Text;
12usingSystem.Collections;
13
14publicpartialclass_Default:System.Web.UI.Page
15{
16protectedvoidPage_Load(objectsender,EventArgse)
17{
18if(!Page.IsPostBack)
19{
20CreateStructure();
21this.gvFoods.DataSource=this.CreateData();
22this.gvFoods.DataBind();
23}
24}
25privateDataTableCreateStructure()
26{
27DataTabledt=newDataTable();
28dt.Columns.Add(newDataColumn("CategoryID",typeof(int)));
29dt.Columns.Add(newDataColumn("CategoryName",typeof(string)));
30dt.Columns.Add(newDataColumn("Price",typeof(int)));
31returndt;
32}
33publicDataSetCreateData()
34{
35DataSetds=newDataSet();
36DataTabledt=this.CreateStructure();
37
38DataRowdrNew=dt.NewRow();
39drNew=dt.NewRow();
40drNew["CategoryID"]=1;
41drNew["CategoryName"]="Apple";
42drNew["Price"]=2;
43dt.Rows.Add(drNew);
44
45drNew=dt.NewRow();
46drNew["CategoryID"]=2;
47drNew["CategoryName"]="Banana";
48drNew["Price"]=3;
49dt.Rows.Add(drNew);
50
51drNew=dt.NewRow();
52drNew["CategoryID"]=3;
53drNew["CategoryName"]="Orange";
54drNew["Price"]=1;
55dt.Rows.Add(drNew);
56
57drNew=dt.NewRow();
58drNew["CategoryID"]=4;
59drNew["CategoryName"]="Radish";
60drNew["Price"]=2;
61dt.Rows.Add(drNew);
62
63drNew=dt.NewRow();
64drNew["CategoryID"]=5;
65drNew["CategoryName"]="Pen";
66drNew["Price"]=3;
67dt.Rows.Add(drNew);
68
69drNew=dt.NewRow();
70drNew["CategoryID"]=6;
71drNew["CategoryName"]="Pencil";
72drNew["Price"]=7;
73dt.Rows.Add(drNew);
74
75drNew=dt.NewRow();
76drNew["CategoryID"]=7;
77drNew["CategoryName"]="Ruler";
78drNew["Price"]=3;
79dt.Rows.Add(drNew);
80
81drNew=dt.NewRow();
82drNew["CategoryID"]=8;
83drNew["CategoryName"]="Eraser";
84drNew["Price"]=5;
85dt.Rows.Add(drNew);
86
87ds.Tables.Add(dt);
88returnds;
89}
90protectedvoidButton1_Click(objectsender,EventArgse)
91{
92StringWritersw=newStringWriter();
93HtmlTextWriterhtw=newHtmlTextWriter(sw);
94this.gvFoods.RenderControl(htw);
95stringstrHtml=sw.ToString().Trim();
96
97stringExcelFileName="FoodList.xls";
98stringFilePhysicialPathName=Request.PhysicalApplicationPath;
99
100//生成的Excel文件名
101stringobjectExcelFileName=Path.Combine(FilePhysicialPathName,ExcelFileName);
102
103if(File.Exists(objectExcelFileName))
104{
105File.Delete(objectExcelFileName);
106}
107FileStreamfs=newFileStream(objectExcelFileName,FileMode.Create);
108BinaryWriterbw=newBinaryWriter(fs,Encoding.GetEncoding("GB18030"));
109bw.Write(strHtml);
110bw.Close();
111fs.Close();
112
113}
114
publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
115{
116//base.VerifyRenderingInServerForm(control);
117}
118}
119