我正在尝试将gridview的内容导出为excel。我有一些代码:
public void ExcelDownload(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=MailingList.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("EN-US", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
MailingList.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
}
在单击链接时运行。这很好,除了它导出整个网页,而不仅仅是gridview(MailingList)。有谁知道如何解决这个问题?