gridview数据导入Excel时的问题

cx_greatoo 2008-12-01 08:21:11
我在将gridview数据导入Excel时,使用下面的代码:
protected void Button6_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
string user = Session["sb_username"].ToString();
string query = "select excel from view_authority where uid='" + user + "'";
SqlCommand mycom = new SqlCommand(query, con);
string uid = Convert.ToString(mycom.ExecuteScalar());
if (uid == "True")
{
Excel();
}
else
{
Response.Write("<script>alert(\"对不起,您没有使用此功能的权限,请联系管理员!\");</script>");
return;
}
con.Close();
}
protected void Excel()
{
Export("application/ms-excel", "设备信息.xls");
}
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);//没有此代码会报错类型"GridView"的控件"GridView1"必须放在具有runat=server 的窗体标记内
}

}
当程序运行到GridView1.RenderControl(hw);时,报错提示:只能在执行 Render() 的过程中调用 RegisterForEventValidation;请问这个要如何解决,在线等,谢谢!
...全文
109 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
HDNGO 2008-12-01
  • 打赏
  • 举报
回复
http://www.cnblogs.com/zyh-nhy/archive/2007/06/06/773624.html

有个类,直接用~
HDNGO 2008-12-01
  • 打赏
  • 举报
回复
你把你可爱的dataset传过去导出excel就可以了~
cx_greatoo 2008-12-01
  • 打赏
  • 举报
回复
有个新问题问一下,就是导出Excel表的数据有“编辑删除”这一列,请问怎么删除这一列呢?
sxmonsy 2008-12-01
  • 打赏
  • 举报
回复
你的gridview1 绑定数据了吗
cx_greatoo 2008-12-01
  • 打赏
  • 举报
回复
这个问题我已经找到答案解决了,另有个新问题问一下,就是导出的数据有“编辑删除”这一列,请问怎么删除这一列呢?
孤独侠客123 2008-12-01
  • 打赏
  • 举报
回复
这种方式导出还行

http://www.cnblogs.com/zhangronghua/archive/2008/11/07/951899.html

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧