【发布时间】:2013-08-21 21:30:57
【问题描述】:
我正在尝试将GridView 导出到 Excel。
我已尝试按照此处找到的步骤进行操作:
- http://www.programming-free.com/2012/09/aspnet-export-grid-view-to-excel.html#.UhUREpK1F9o
- export gridview to excel file
和其他类似的网站。
我的GridView 没有任何不同于默认值的特殊属性
如果这很重要,我的SqlDataSource 使用filterExpression。
当我尝试上述解决方案时,没有出现异常,但 excel 不生产。
更新
我忘了提到GridView 在asp:Content 控件中。我听说
这可能很重要。
我的代码隐藏是这样的(我尝试了多种方法)。
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = String.Empty;
EnableViewState = false;
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);
GridView3.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
【问题讨论】:
-
你想如何导出它?至少显示代码隐藏。你用调试器看看会发生什么?
-
@我用过调试器,和我说的一样,没有异常,一切运行顺利,除了页面不显示。
-
但是这个方法被调用了不是吗?另外,我假设你已经实现了
VerifyRenderingInServerForm。 -
@TimSchmelter 是的,它是在单击按钮时调用的。该按钮也位于
asp:Content中,但不在GridView中 -
所以该按钮位于位于 MasterPage 的
Content中的 GridView(具体在哪里?)?你看到我关于VerifyRenderingInServerForm的问题了吗?你没有使用 ASP.NET-Ajax,是吗?