简体   繁体   English

将datagridview导出到Excel时出现问题

[英]Issues exporting datagridview to excel

I'm trying to do an export of a datagrid to excel. 我正在尝试导出datagrid以使其表现出色。 For some reason, known working methods aren't working. 由于某些原因,已知的工作方法不起作用。 The export is done from a user control. 导出是通过用户控件完成的。 My page (default.aspx) uses a master page and the page has a user control that actually has the datagrid I'm trying to export. 我的页面(default.aspx)使用一个母版页面,并且该页面具有一个用户控件,该控件实际上具有我要导出的datagrid。

Here's my code on the ascx: 这是我在ascx上的代码:

Response.ClearContent();
 Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
 StringWriter sw = new StringWriter();
 HtmlTextWriter htw = new HtmlTextWriter(sw);
_gvwResults.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

On my default.aspx (page the holds the ascx) is this code: 在我的default.aspx(页面上保存ascx)上是以下代码:

public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. - required to export file */
}

Here's the error I receive: Sys.Webforms.pagerequestmanagerparsererrorexception. 这是我收到的错误:Sys.Webforms.pagerequestmanagerparsererrorexception。 The message received from the server could ot be parsed. 可以解析从服务器接收到的消息。 Common causes for this error are when the response is modified by calls to Response.Write(), response filters, httpmodules or server trace is enabled. 导致此错误的常见原因是通过调用Response.Write()修改响应,启用响应筛选器,httpmodules或服务器跟踪。

Any ideas? 有任何想法吗? This code should work but it's almost as if the response object is not being cleared. 该代码应该可以工作,但是几乎就像没有清除响应对象一样。 Ideas? 有想法吗?

Turns out that the since the combobox was ajaxified, the export was not happening. 事实证明,由于组合框已被邻接,因此未发生导出。 The event was firing but, as the error message says, it was response.writing onto the existing page, thus throwing the error, which would not allow for a new document (xls in this case) to be rendered. 事件正在触发,但是,如错误消息所述,它是在现有页面上的response.write,因此引发了错误,这将不允许呈现新文档(在这种情况下为xls)。 After setting the combobox to do a postback on the page and un-ajaxifying it, the export started working.. 将组合框设置为在页面上执行回发并取消对它的ajaxize后,导出开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM