我使用下面的代碼導出我GridView
到Excel
:ASP.Net網格導出到Excel
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvReports.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
此代碼是從this網站複製。我遵循了所有的指示,但在執行任何事情之後。沒有例外(除Thread.Abort
,我認爲這是因爲Response.End()
)。
我也用Response.Flush()
但什麼也沒有發生也不例外或文件等
感謝。
編輯現在,代碼如下如下:
我的GridView中控制。該控件在asp.net頁面上,該頁面具有以下方法。按鈕在ascx控件
public override void VerifyRenderingInServerForm(Control control)
{
}
點擊事件:
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
ExportToExcel();
}
private void ExportToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvReports.AllowPaging = false;
gvReports.DataBind();
gvReports.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
gvReports沒有鏈接,複選框等。5個綁定列和1個按鈕。
什麼都沒有發生。
gvReports。的DataBind();應該叫做 – MMK
@MMK – Kashif