100分求助!急~~~~在线等。。

syb1045 2009-09-03 02:58:16
我在用户控件中放入有UpdatePanel. 里面有GridView , 现在我想把它的数据导入到Excel文档。

public void ExportResult(DataSet ds)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
for (int i = 0; i < ds.Tables.Count; i++)
{
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[i];
dg.DataBind();
dg.RenderControl(htmlWrite);
}
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=text.xls");

HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.End();
}

protected void Button2_Click(object sender, EventArgs e)
{

ExportResult(ds);

}
调用该方法后,提示好像是说Response.Write()那里出错了。到网上找信息。 说要重写
public override void VerifyRenderingInServerForm(Control control){}
可是我重写这个方法却提示找不到合适的方法来重写, 继续上网查 说改EnableEventValidation = "false".
改了 但是还是没效果。。。
请问我改如何才能解决这个问题。 。 帮帮忙。。。谢谢了。。
有点乱,不知道说清楚没。

...全文
209 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
syb1045 2009-09-03
  • 打赏
  • 举报
回复
完事。结贴。。 谢谢大家了 。
John_Hee 2009-09-03
  • 打赏
  • 举报
回复
http://tmsoft.lsxy.com/index.php?load=read&id=415

满足你对excel 的操作。

接着下载完事吧
txg92 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 syb1045 的回复:]
知道有什么好的方法去除导入到Excel文档后所显示的列名吗?
[/Quote]
如果用我的那种导出EXCEL方式,将DataGird2(外面的那个)的ShowHeader的属性设为false就OK了
syb1045 2009-09-03
  • 打赏
  • 举报
回复
知道有什么好的方法去除导入到Excel文档后所显示的列名吗?
syb1045 2009-09-03
  • 打赏
  • 举报
回复
谢谢各位。 我再试试、、
txg92 2009-09-03
  • 打赏
  • 举报
回复
对了
调用该函数的页面还要重载这个方法才行
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
fwqkey 2009-09-03
  • 打赏
  • 举报
回复
拿10分走人
txg92 2009-09-03
  • 打赏
  • 举报
回复
UpdatePanel套有GridView,然后将这个 GridView导出Excel,这肯定不行,我试过很多次,不能放到UpdatePanel中

解决方案
在UpdatePanel外面再放一个GridView2(导出的时候使用一下不会影响界面,也不会显示在界面),用于导出Excel,只是起到一个中转的作用

你用我这个函数去导,还可以写EXCEL的表头和表尾
 /// <summary>
/// 导出Excel静态方法
/// </summary>
/// <param name="gv">gridView控件</param>
/// <param name="ds">数据源DataSet</param>
/// <param name="tableHead">表头,就是表的标题</param>
/// <param name="tableName">表名</param>
public static void OutExcel(GridView gv, DataSet ds, string tableHead, string tableName)
{

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
if (!String.IsNullOrEmpty(tableName))
{ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(tableName) + ".xls"); }
else
{ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=考试信息表.xls"); }
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
if (ds.Tables[0].Rows.Count > 0)
{ HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); }
//this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
GridView excel = new GridView();
//设置显示样式
System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
AlternatingStyle.BackColor = System.Drawing.Color.FromArgb(233, 234, 244);
headerStyle.BackColor = System.Drawing.Color.LightGray;
headerStyle.Font.Bold = true;
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
gv.AlternatingRowStyle.MergeWith(AlternatingStyle);
gv.HeaderStyle.MergeWith(headerStyle);
gv.RowStyle.MergeWith(itemStyle);
gv.GridLines = GridLines.Both;
gv.HeaderStyle.Font.Bold = true;
gv.Font.Size = System.Web.UI.WebControls.FontUnit.Point(9);
gv.DataSource = ds.Tables[0];
gv.DataBind();
System.Text.StringBuilder ExcelTable = new StringBuilder();
if (ds.Tables[0].Rows.Count > 0)
{
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.RenderControl(oHtmlTextWriter);
//System.Text.StringBuilder ExcelTable = new System.Text.StringBuilder("<div align='center' style='height:50px'><br><b>" + tableHead + "</b></div><div align='center' style='height:50px'><br></div>");
ExcelTable.Append("<div align='center' style='height:50px'><br><b>" + tableHead + "</b></div><div align='center' style='height:50px'><br></div>");
ExcelTable.Append(oStringWriter.ToString());
ExcelTable.Append("<div align='right' style='height:50px;font-size:12px'>创建时间:" + DateTime.Now.ToShortDateString() + "</div>");

}

HttpContext.Current.Response.Write(ExcelTable);
HttpContext.Current.Response.End();
}


使用的时候,将UpdatePanel外面的那个GridView2传到参数里面去

我这函数经过调试,楼主复制下来,不需要修改就可以用
syb1045 2009-09-03
  • 打赏
  • 举报
回复
我用的就是原始生成的。但是它提示找不到合适的方法来重写。可能是用户控件的原因。
chen_ya_ping 2009-09-03
  • 打赏
  • 举报
回复
原始生成的如下的代码:
public override void VerifyRenderingInServerForm(Control control){}
就用原始生成的不用自己写任何的东西
syb1045 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 john_hee 的回复:]
是要提供导出下载吗?
HttpContext.Current.Response.Write(stringWrite.ToString());是不能实现的
要使用
HttpContext.Current.Response.WriteFile(FileName); // 文件路径

UpdatePanel 刷新下载。。。。。
不如建一个Page 或者 用 HttpHandler 来处理导出下载吧

http://www.cnblogs.com/lxinxuan/archive/2007/07/09/545533.html
[/Quote]

你说的方法我去试试看
John_Hee 2009-09-03
  • 打赏
  • 举报
回复
是要提供导出下载吗?
HttpContext.Current.Response.Write(stringWrite.ToString());是不能实现的
要使用
HttpContext.Current.Response.WriteFile(FileName); // 文件路径

UpdatePanel 刷新下载。。。。。
不如建一个Page 或者 用 HttpHandler 来处理导出下载吧

http://www.cnblogs.com/lxinxuan/archive/2007/07/09/545533.html
syb1045 2009-09-03
  • 打赏
  • 举报
回复
何解? 改怎么去解决我的问题?
dd__dd 2009-09-03
  • 打赏
  • 举报
回复
在UpdatePanel里是不能使用Response的
syb1045 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhulong1111 的回复:]
看你的项目引用了office 的 excel 组件没??
[/Quote]
我在别的页面用这个方法可以到处数据到Excel文档。 已经测试过了 。还要引用吗。?
syb1045 2009-09-03
  • 打赏
  • 举报
回复
我这样做的啊。 但是提示错误啊
gwf25sz 2009-09-03
  • 打赏
  • 举报
回复
只要把这个方法拷贝过去,什么都不用改``````````

顺便提一下,用这种方法导出,无法导出图片
zhulong1111 2009-09-03
  • 打赏
  • 举报
回复
看你的项目引用了office 的 excel 组件没??
zhulong1111 2009-09-03
  • 打赏
  • 举报
回复
你把这个方法放在后台就行了,别管它

62,025

社区成员

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

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

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

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