GridView导出Execl
当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示。
有两种方法可以解决以上问题:
1.修改web.config(不推荐)<pages enableEventValidation ="false" ></pages>
2.直接在导出Execl的页面修改
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"
CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>
附:Gridview导出Execl可分页全部导出代码(C#)
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11

12
using System.Data.SqlClient;13
using System.IO;14
using System.Text;15
using System.Data.OleDb;16

17
public partial class GridView_ReplaceString : System.Web.UI.Page18


{19
public string strConn = ConfigurationManager.AppSettings["strConnNorthwind"];20
protected void Page_Load(object sender, EventArgs e)21

{22
//为GridView中的数据添加自动/正常换行属性23
//GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");24
//下面这行是自动换行25
//GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");26

27
if (!IsPostBack)28

{29
GridViewBind();30
Master.MasterTitle = "代替字符";31
}32

33
}34
public void GridViewBind()35

{36
string sqlstr = "select * from Categories";37
SqlConnection sqlcon = new SqlConnection(strConn);38
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);39
DataSet myds = new DataSet();40
sqlcon.Open();41
myda.Fill(myds, "Categories");42
GridView1.DataSource = myds;43
GridView1.DataBind();44

45
//也可以在RowDataBond事件中实现46
//for (int i = 0; i <= GridView1.Rows.Count - 1; i++)47
//{48
// DataRowView mydrv;49
// string gIntro;50
// if (GridView1.PageIndex == 0)51
// {52
// mydrv = myds.Tables["Categories"].DefaultView[i];53
// gIntro = Convert.ToString(mydrv["Description"]);54
// GridView1.Rows[i].Cells[2].Text = SubStr(gIntro, 2);55
// }56
// else57
// {58
// mydrv = myds.Tables["Categories"].DefaultView[i + (5 * GridView1.PageIndex)];59
// gIntro = Convert.ToString(mydrv["Description"]);60
// GridView1.Rows[i].Cells[2].Text = SubStr(gIntro, 2);61
// }62
//}63
sqlcon.Close();64
}65
public string SubStr(string sString, int nLeng)66

{67
if (sString.Length <= nLeng)68

{69
return sString;70
}71
string sNewStr = sString.Substring(0, nLeng);72
sNewStr = sNewStr + "
";73
return sNewStr;74
}75
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)76

{77
GridView1.PageIndex = e.NewPageIndex;78
GridViewBind();79
}80
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)81

{82
//这种方法更简单,所以要改变数据的呈现方式都在这个事件里改83
//for (int i = 0; i <= GridView1.Rows.Count - 1; i++)84
//{85
// string gIntro = GridView1.Rows[i].Cells[2].Text;86
// GridView1.Rows[i].Cells[2].Text = SubStr(gIntro, 2);87
//}88

89
//为GridView数据行添加鼠标移出移近效果90
if (e.Row.RowType == DataControlRowType.DataRow)91

{92
e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='lightBlue'");93
e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;");94
}95
96
}97

98
//导出GridView数据至Excel的两种方式99
protected void btnGToE_Click(object sender, EventArgs e)100

{101
//Export("application/ms-excel", "Categories.xls");//此方法要成功可在web.config中配置102

103

104
//105
Response.Clear();106
Response.AddHeader("content-disposition", "attachment;filename=" +107
HttpUtility.UrlEncode("Categories.xls", Encoding.UTF8).ToString());108
Response.Charset = "";109

110
// If you want the option to open the Excel file without saving than comment out the line below111

112
// Response.Cache.SetCacheability(HttpCacheability.NoCache);113

114
Response.ContentType = "application/vnd.xls";115
System.IO.StringWriter stringWrite = new System.IO.StringWriter();116
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);117

118
// turn off paging 119
GridView1.AllowPaging = false;120
GridViewBind();121

122
GridView1.RenderControl(htmlWrite);123

124
Response.Write(stringWrite.ToString());125
Response.End();126

127
// turn the paging on again 128
GridView1.AllowPaging = true;129
GridViewBind();130
}131
private void Export(string FileType, string FileName)132

{133
Response.Charset = "GB2312";134
Response.ContentEncoding = System.Text.Encoding.UTF7;135
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());136
Response.ContentType = FileType;137
this.EnableViewState = false;138
StringWriter tw = new StringWriter();139
HtmlTextWriter hw = new HtmlTextWriter(tw);140
GridView1.RenderControl(hw);141
Response.Write(tw.ToString());142
Response.End();143
}144
public override void VerifyRenderingInServerForm(Control control)145

{146

147
}148

149
//从Excel导入数据至GridView150
//存在的问题是Excel格式151
protected void btnEToG_Click(object sender, EventArgs e)152

{153
GridView2.DataSource = CreateDataSource();154
GridView2.DataBind();155
}156
private DataSet CreateDataSource()157

{158
string strCon;159
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("EXCEL.XLS") + "; Extended Properties=Excel 8.0;";160
OleDbConnection olecon = new OleDbConnection(strCon);161
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);162
DataSet myds = new DataSet();163
myda.Fill(myds);164
return myds;165
}166
}167

浙公网安备 33010602011771号