亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的導出到 Excel 功能會導出整個頁面?

為什么我的導出到 Excel 功能會導出整個頁面?

C#
呼喚遠方 2023-09-24 16:30:56
我正在嘗試導出顯示在窗口(模式)中的網格視圖,但它導出整個頁面。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();}
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

問題是我在 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;一開始的代碼行剛剛解決了我的整個問題。


查看完整回答
反對 回復 2023-09-24
  • 1 回答
  • 0 關注
  • 207 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號