为什么我的导出到 Excel 功能会导出整个页面?

我正在尝试导出显示在窗口(模式)中的网格视图,但它导出整个页面。


public void ExportToXLS(GridView gv)

{

    gv.AllowPaging = false;

    HttpContext.Current.Response.Buffer = true;

    HttpContext.Current.Response.ClearContent();

    HttpContext.Current.Response.ClearHeaders();

    HttpContext.Current.Response.Clear();

    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridView.xls");

    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

    GridView gvExp = new GridView();

    gvExp = gv;

    gvExp.RenderControl(htmlWrite);

    HttpContext.Current.Response.Write(stringWrite.ToString());

    HttpContext.Current.Response.Flush();

    HttpContext.Current.Response.Close();

    HttpContext.Current.Response.End();

}


呼唤远方
浏览 81回答 1
1回答

慕容森

问题是我在 gridview 中有一个链接按钮,它导致页面无法正确呈现。解决方案很简单,我只是删除了链接按钮认为我确实不需要它们的列。public void ExportToXLS(GridView gv){    GV.Columns[4].Visible = false;    GV.Columns[5].Visible = false;    gv.AllowPaging = false;    HttpContext.Current.Response.Buffer = true;    HttpContext.Current.Response.ClearContent();    HttpContext.Current.Response.ClearHeaders();    HttpContext.Current.Response.Clear();    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridView.xls");    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";    System.IO.StringWriter stringWrite = new System.IO.StringWriter();    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);    GridView gvExp = new GridView();    gvExp = gv;    gvExp.RenderControl(htmlWrite);    HttpContext.Current.Response.Write(stringWrite.ToString());    HttpContext.Current.Response.Flush();    HttpContext.Current.Response.Close();    HttpContext.Current.Response.End();}GV.Columns[ ].Visible = false;一开始的代码行刚刚解决了我的整个问题。
打开App,查看更多内容
随时随地看视频慕课网APP