ASP.Net 2.0: Export GridView to Excel
【原文见: http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx】
Author: Dipal Choksi
Translator: SPARON
T-Blog: http://sparon.cnblogs.com
T-MSN: ZhaoKeYong@hotmail.com
本文描述了在ASP.NET 2.0中如何将GridView 导出为 Excel
简介:
在本文中我们将具体介绍如何将ASP.Net 2.0下的GridView导出为Excel.
本文的焦点是Gridview导为Excel功能和它的数据绑定只出口示范功能.
本文代码可以用来导出为Excel的功能但不局限于这个部分,还可以在很多项目中使用。
Step 1: Setup your web page with the Gridview
这里我假定你满足:在一个页面中有个名为GridView1的GridView。在GridView中我们绑定了一个名为ContactPhone的SQL数据库。接下来的代码就是如何将GridView导出为Excel并且不依赖于具体的数据绑定还具有场景自改变能力。
ContactPhone Table Structure:
Column Name
|
Type
|
ContactID
|
Int (Identity)
|
FName
|
Varchar(50)
|
LName
|
Varchar(50)
|
ContactPhone
|
Varchar(20)
|
Step: The Actual Export
这段代码是直接输出为Excel的,你也可以改变content-disposition和ContentType以输出不同的类型。
1
string attachment = "attachment; filename=Contacts.xls";
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
Response.ClearContent();
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
Response.AddHeader("content-disposition", attachment);
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
Response.ContentType = "application/ms-excel";
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
StringWriter sw = new StringWriter();
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
HtmlTextWriter htw = new HtmlTextWriter(sw);
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
GridView1.RenderControl(htw);
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
Response.Write(sw.ToString());
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
Response.End();
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如果你运行以上代码,将返回一个HttpException:
'GridView1'是一个类型为'GridView'的控件,必须为其添加一个runat=server标记.
为避免这个错误,我们添加以下代码:
1
public override void VerifyRenderingInServerForm(Control control)
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
}
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Step : Convert the contents
如果GridView中有其它控件,比如Checkboxes,Dropdownlists,我们需要将它转换为其相关的值,以下递归就用于导出Excel前的准备工作,将各类控件转换为其相关值.
1
private void PrepareGridViewForExport(Control gv)
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
LinkButton lb = new LinkButton();
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
Literal l = new Literal();
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
string name = String.Empty;
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
for (int i = 0; i < gv.Controls.Count; i++)
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
if (gv.Controls[i].GetType() == typeof(LinkButton))
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
l.Text = (gv.Controls[i] as LinkButton).Text;
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
gv.Controls.Remove(gv.Controls[i]);
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
gv.Controls.AddAt(i, l);
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
else if (gv.Controls[i].GetType() == typeof(DropDownList))
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
gv.Controls.Remove(gv.Controls[i]);
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
gv.Controls.AddAt(i, l);
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
}
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
else if (gv.Controls[i].GetType() == typeof(CheckBox))
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
l.Text = (gv.Controls[i] as CheckBox).Checked? "True" : "False";
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
gv.Controls.Remove(gv.Controls[i]);
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
gv.Controls.AddAt(i, l);
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
if (gv.Controls[i].HasControls())
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
PrepareGridViewForExport(gv.Controls[i]);
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
}
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Code Listing:
Image: Page Design
![](https://www.cnblogs.com/images/cnblogs_com/sparon/A1.jpg)
Image : Sample in action
![](https://www.cnblogs.com/images/cnblogs_com/sparon/a_2.jpg)
Image: Export to Excel button is clicked
![](https://www.cnblogs.com/images/cnblogs_com/sparon/a_3.jpg)
Image: GridView contents exported to Excel
![](https://www.cnblogs.com/images/cnblogs_com/sparon/a_4.jpg)
ExcelExport.aspx
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportExcel.aspx.cs" Inherits="DeleteConfirm" %>
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
<html xmlns="http://www.w3.org/1999/xhtml" >
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
<head runat="server">
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
<title>Contacts Listing</title>
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
</head>
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
<body>
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
<form id="form1" runat="server">
20![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
<div>
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
<strong><span style="font-size: small; font-family: Arial; text-decoration: underline">
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
Contacts Listing
26![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Export To Excel" /></span></strong><br />
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
<br />
30![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ContactID"
32![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." style="font-size: small; font-family: Arial" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
34![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
<Columns>
36![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
<asp:BoundField DataField="ContactID" HeaderText="ContactID" ReadOnly="True" SortExpression="ContactID" Visible="False" />
38![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
<asp:BoundField DataField="FName" HeaderText="First Name" SortExpression="FName" />
40![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
<asp:BoundField DataField="LName" HeaderText="Last Name" SortExpression="LName" />
42![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
<asp:BoundField DataField="ContactPhone" HeaderText="Phone" SortExpression="ContactPhone" />
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
<asp:TemplateField HeaderText="Favorites">
46![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
<ItemTemplate>
48![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
50![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
<asp:CheckBox ID="CheckBox1" runat="server" />
52![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
</ItemTemplate></asp:TemplateField>
54![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
</Columns>
56![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
<FooterStyle BackColor="#CCCC99" />
58![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
<RowStyle BackColor="#F7F7DE" />
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
62![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
63
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
64![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
66![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
<AlternatingRowStyle BackColor="White" />
68![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
</asp:GridView>
70![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
71
72![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
73
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ContactsConnectionString1 %>"
74![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
75
DeleteCommand="DELETE FROM [ContactPhone] WHERE [ContactID] = @ContactID" InsertCommand="INSERT INTO [ContactPhone] ([FName], [LName], [ContactPhone]) VALUES (@FName, @LName, @ContactPhone)"
76![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
77
ProviderName="<%$ ConnectionStrings:ContactsConnectionString1.ProviderName %>"
78![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79
SelectCommand="SELECT [ContactID], [FName], [LName], [ContactPhone] FROM [ContactPhone]"
80![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
81
UpdateCommand="UPDATE [ContactPhone] SET [FName] = @FName, [LName] = @LName, [ContactPhone] = @ContactPhone WHERE [ContactID] = @ContactID">
82![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
83
<InsertParameters>
84![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85
<asp:Parameter Name="FName" Type="String" />
86![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
87
<asp:Parameter Name="LName" Type="String" />
88![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
<asp:Parameter Name="ContactPhone" Type="String" />
90![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91
</InsertParameters>
92![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
93
<UpdateParameters>
94![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
95
<asp:Parameter Name="FName" Type="String" />
96![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
97
<asp:Parameter Name="LName" Type="String" />
98![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
99
<asp:Parameter Name="ContactPhone" Type="String" />
100![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
101
<asp:Parameter Name="ContactID" Type="Int32" />
102![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
103
</UpdateParameters>
104![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
105
<DeleteParameters>
106![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
107
<asp:Parameter Name="ContactID" Type="Int32" />
108![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
109
</DeleteParameters>
110![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
111
</asp:SqlDataSource>
112![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
113
114![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
115
<br />
116![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
117
</div>
118![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
119
</form>
120![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
121
</body>
122![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
123
</html>
124![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
125![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ExcelExport.aspx.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
using System.Data;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
using System.Configuration;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
using System.Collections;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
using System.Web;
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
using System.Web.Security;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
using System.Web.UI;
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
using System.Web.UI.WebControls;
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
using System.Web.UI.WebControls.WebParts;
18![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
using System.Web.UI.HtmlControls;
20![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
using System.Text;
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
using System.IO;
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
26![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
public partial class DeleteConfirm : System.Web.UI.Page
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
protected void Page_Load(object sender, EventArgs e)
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
}
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
protected void Button1_Click(object sender, EventArgs e)
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
//Export the GridView to Excel
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
PrepareGridViewForExport(GridView1);
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
ExportGridView();
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
private void ExportGridView()
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
string attachment = "attachment; filename=Contacts.xls";
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
Response.ClearContent();
62![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
Response.AddHeader("content-disposition", attachment);
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
Response.ContentType = "application/ms-excel";
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
StringWriter sw = new StringWriter();
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
HtmlTextWriter htw = new HtmlTextWriter(sw);
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
GridView1.RenderControl(htw);
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
Response.Write(sw.ToString());
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
Response.End();
76![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
public override void VerifyRenderingInServerForm(Control control)
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
private void PrepareGridViewForExport(Control gv)
90![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
LinkButton lb = new LinkButton();
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
Literal l = new Literal();
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
string name = String.Empty;
98![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
for (int i = 0; i < gv.Controls.Count; i++)
100![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
102![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
if (gv.Controls[i].GetType() == typeof(LinkButton))
104![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
l.Text = (gv.Controls[i] as LinkButton).Text;
108![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
109
gv.Controls.Remove(gv.Controls[i]);
110![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
111
gv.Controls.AddAt(i, l);
112![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
}
114![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
else if (gv.Controls[i].GetType() == typeof(DropDownList))
116![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
120![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
121
gv.Controls.Remove(gv.Controls[i]);
122![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
123
gv.Controls.AddAt(i, l);
124![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
}
126![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
else if (gv.Controls[i].GetType() == typeof(CheckBox))
128![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
132![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133
gv.Controls.Remove(gv.Controls[i]);
134![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
135
gv.Controls.AddAt(i, l);
136![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137
}
138![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
139
if (gv.Controls[i].HasControls())
140![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
141![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
PrepareGridViewForExport(gv.Controls[i]);
144![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
145
}
146![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
147
}
148![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
149
}
150![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
151
}
152![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
153![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Implementation Options:
通常情况,在输出函数中开发人员都会面临一个错误,典型的就是"RegisterForEventValidation can only be called during Render();"
访问者通常会在评论中提出一些好的建议.我特别要强调的是开发者需要重写VerifyRenderingInServerForm方法,该方法描述如下:
- Step 1:实现导出功能的上述功能.
- Step 2:重写一个VerifyRenderingInServerForm的空方法.
- Step 3:修改ExportGridView函数,在绿色高亮代码部创建HtmlForm【原句为:The code highlighted in green creates and HtmlForm on the fly,在翻译HtmlForm on the fly时遇到了一些困难,故on the fly未翻译,请各位高手指教】,在导出girdview之前,添加gridview 到新的form并且render它(取代原来的render实现)
1
private void ExportGridView()
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
string attachment = "attachment; filename=Contacts.xls";
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
Response.ClearContent();
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
Response.AddHeader("content-disposition", attachment);
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
Response.ContentType = "application/ms-excel";
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
StringWriter sw = new StringWriter();
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
HtmlTextWriter htw = new HtmlTextWriter(sw);
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
// Create a form to contain the grid
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
HtmlForm frm = new HtmlForm();
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
GridView1.Parent.Controls.Add(frm);
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
frm.Attributes["runat"] = "server";
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
frm.Controls.Add(GridView1);
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
frm.RenderControl(htw);
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
//GridView1.RenderControl(htw);
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
Response.Write(sw.ToString());
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
Response.End();
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
}
40![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这样实施有个优势,就是可将其设置为复用代码类库,不用每次去复写基类的方法.
Note to readers:
Thank you for your comments and feedback! Happy coding!!!
ASP.Net 2.0: Export GridView to Excel - Part II
该文中将会在导出Excel 时GridView引入Hyperlink列,以至于需要使用更多的反射来重新设计原来的逻辑.
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.