您的位置:首页 > 其它

解决gridview的excel导出问题

2005-12-30 18:11 393 查看
以前datagrid导出为excel,只需如下代码:

Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");

Response.Charset =
"";

// If you want
the option to open the Excel file without saving than

// comment out
the line below

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

Response.ContentType =
"application/vnd.xls";

System.IO.StringWriter
stringWrite = new
System.IO.StringWriter();

System.Web.UI.HtmlTextWriter
htmlWrite = new
HtmlTextWriter(stringWrite);

gvMaster.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

现在改成gridview后,以上代码会提示异常错误,经网上搜索,解决方案如下

1、在导出界面中重载
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
2、在web.config中修改<pages enableEventValidation ="false" ></pages>

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

3、现在可以正常导出了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: