需要注意两个地方:
1.EnableEventValidation="false" 必须加
2.下面这个事件必须加
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
在我的资源里有相应的原代码下载
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="excel.aspx.cs" EnableEventValidation="false" Inherits="将GridView导出到Excel_excel" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>将Gridview导出到Excel,Word</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="stuid" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="stuid" HeaderText="编号" InsertVisible="False" ReadOnly="True"
SortExpression="stuid" />
<asp:BoundField DataField="stuname" HeaderText="姓名" SortExpression="stuname" />
<asp:CheckBoxField DataField="stusex" HeaderText="性别" SortExpression="stusex" />
<asp:BoundField DataField="stuaddress" HeaderText="地址" SortExpression="stuaddress" />
<asp:BoundField DataField="stuage" HeaderText="年龄" SortExpression="stuage" />
<asp:BoundField DataField="stutuition" HeaderText="学费" SortExpression="stutuition" />
<asp:BoundField DataField="stuaveragescore" HeaderText="成绩" SortExpression="stuaveragescore" />
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myTestGridViewConnectionString %>"
SelectCommand="SELECT * FROM [stu]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="导出到Excel" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="导出到Word" Width="108px" />
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class 将GridView导出到Excel_excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void ExportToExcel(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//导出到Excel
protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel","学生信息.xls");
}
//导出到Word
protected void Button2_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel", "学生信息.doc");
//ExportToExcel("application/ms-word", "学生信息.doc");//都可以
}
//这个事件必须加
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
}
当然上面的这些东东也是我从CSDN上面学来的,有一篇是看的青青月儿的,还有一篇,忘了是谁的啦,反正挺感谢的。