【发布时间】:2013-02-18 17:35:22
【问题描述】:
我有这个代码:
protected void ibtGenerateReport_Click(object sender, ImageClickEventArgs e)
{
string filename = "report.xls";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = odsLSRAudit;
DataGrd.DataBind();
DataGrd.RenderControl(htmlWrite);
System.IO.StreamWriter vw = new System.IO.StreamWriter(filename, true);
stringWriter.ToString().Normalize();
vw.Write(stringWriter.ToString());
vw.Flush();
vw.Close();
WriteAttachment(filename, "application/vnd.ms-excel", stringWriter.ToString());
}
public static void WriteAttachment(string FileName, string FileType, string content)
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = FileType;
Response.Write(content);
Response.End();
}
但是Response.End() 给了我以下错误:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not 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.
Details: Error parsing near '<table cellspacing="'.
Response.Write(content) 有我所看到的正确信息。但是除了上述错误之外,没有出现保存/打开对话框。
我正在使用更新面板。
【问题讨论】:
-
您使用的是 updatepanels 还是 asp.net ajax?试试这个stackoverflow.com/questions/11221033/…
标签: c# asp.net gridview export-to-excel