GridView导出到EXCEL出错,请求帮忙!

tianhw1980 2009-04-14 05:00:13
在WEB界面上的GridView导出到EXCEL出错,请求帮忙!

导出的代码是在网上找的

protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{ }

但是报错,在
this.GridView1.RenderControl(oHtmlTextWriter);
说:
只能在执行Render()的过程中调用RegisterForEventValidation;


...全文
89 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
happy664618843 2009-04-19
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false"%>
把EnableEventValidation属性设为False就可以了!
major 2009-04-18
  • 打赏
  • 举报
回复
导出EXCEL方法
public override void VerifyRenderingInServerForm(Control control)
{

//base.VerifyRenderingInServerForm(control);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
ReportToExcel(GridView1,"测试将GridView导出到Excel中");
}
public static void ReportToExcel(GridView gv, string FileName)
{
string fileName = FileName + ".xls";
gv.HeaderStyle.ForeColor = Color.Black;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";
ClearChildControls(gv);
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gv.Visible = true;
gv.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private static void RecursiveClear(Control control)
{

for (int i = control.Controls.Count - 1; i >= 0; i--)
{
RecursiveClear(control.Controls[i]);
}

if (control is Repeater)
{
control.Parent.Controls.Remove(control);
}
else if (control is LinkButton)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = ((LinkButton)control).Text;
control.Parent.Controls.Remove(control);
}
else if (control is Button)
{
control.Parent.Controls.Remove(control);
}

else if (control is System.Web.UI.WebControls.Image)
{
if (((System.Web.UI.WebControls.Image)control).Visible)
{
control.Parent.Controls.Add(new LiteralControl("<span style='font-size:8px;'>o</span>"));
}
control.Parent.Controls.Remove(control);
}
else if (control is ListControl)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = ((ListControl)control).SelectedItem.Text;
}
catch
{
}
control.Parent.Controls.Remove(control);

}

return;
}
protected static void ClearChildControls(Control dg1)
{
if (dg1 is GridView)
{
GridView dg = (GridView)dg1;
for (int i = dg.Columns.Count - 1; i >= 0; i--)
{
if (dg.Columns[i].GetType().Name == "ButtonColumn"
|| dg.Columns[i].GetType().Name == "CheckBoxField"
|| dg.Columns[i].GetType().Name == "CommandField")
{
dg.Columns[i].Visible = false;
}
}
RecursiveClear(dg1);
}
else
{
RecursiveClear(dg1);
}
}
沉序员 2009-04-18
  • 打赏
  • 举报
回复
DING
「已注销」 2009-04-18
  • 打赏
  • 举报
回复
这里有你要的答案 好好学习 http://www.olcodes.com/article/html/5207.html GridView 的所以基础功能
tianhw1980 2009-04-16
  • 打赏
  • 举报
回复
我想的是实现一个导入导出

比如:A地的使用者导出了数据,只要送一个EXCEL文件到B地

那么B地就可以把数据导入数据库了

所以这是没办法的啊!






clxsl_huang 2009-04-16
  • 打赏
  • 举报
回复
直接在GridView上取不可以吗?
如果要在EXCEL中取的话,可以直接读取excel中的数据,不过这样比较麻烦
tianhw1980 2009-04-16
  • 打赏
  • 举报
回复
好的,问题解决了,谢谢大家

现在我想通过一个按钮,将导出的数据的某一行某一列传入到页面的一个TextBox上显示,

该怎么做呢?

比如,我只导出了一行数据,但是EXCEL会生成一个表头,数据在第二行,

我想要获取第二行的第二列

该怎么办?
tianhw1980 2009-04-16
  • 打赏
  • 举报
回复
好的,问题解决了,谢谢大家

现在我想的是通过一个按钮,将导出的数据的某一行传入到页面的一个TextBox上显示,

该怎么做呢?

比如,我只导出了一行数据,但是EXCEL会生成一个表头,数据在第二行,

我想要获取第二行的第二列

该怎么办?
dufan88521 2009-04-14
  • 打赏
  • 举报
回复
protected void 导出Excel(object sender, EventArgs e)
{


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=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView列表.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
tianhw1980 2009-04-14
  • 打赏
  • 举报
回复
很困惑啊
整整一天了,就是不明白错在哪里啊

大家帮帮我这只小菜鸟吧

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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