(一) . 运行示例图
1. 待导出数据的GridView图:
2. 生成的Excel文件
(二). 代码
1. 前台页面 GridViewToExcelFile.aspx 代码:
1
<%@ Page Language="C#" AutoEventWireup="true"
EnableEventValidation="false" CodeFile="GridViewToExcelFile.aspx.cs" Inherits="_Default" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>Untitled Page</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<asp:GridView ID="gvFoods" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" EnableViewState="true">
13
<FooterStyle BackColor="Tan" />
14
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
15
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
16
<HeaderStyle BackColor="Tan" Font-Bold="True" />
17
<AlternatingRowStyle BackColor="PaleGoldenrod" />
18
</asp:GridView>
19
<br />
20
<asp:Button ID="Button1" runat="server" BackColor="#C0C0FF" OnClick="Button1_Click"
21
Text="Create Excel" Width="137px" />
22
</div>
23
</form>
24
</body>
25
</html>
2. 页面后台文件 GridViewToExcelFile.aspx.cs 代码:
(三). 示例代码下载