匯出excel功能:VS2005

摘要:匯出excel功能:VS2005

匯出excel功能:

型別 'GridView' 的控制項 'GridView1' 必須置於有 runat=server 的表單標記之中。

問題一:可以在您的CodeFile中加入以下這段來解決

    public override void VerifyRenderingInServerForm(Control control)

    {

        //處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中

    }

問題二:如果您的GridView沒有設定分頁,應該就可以正常的執行了。但是如果有設定分頁,很快的又遇到了另外一個問題!出現以下的錯誤訊息:

RegisterForEventValidation 只能在 Render(); 期間呼叫這個問題,

可以設定aspx原始檔中<%Page%>的以下兩個設定解決:EnableEventValidation = "false" AutoEventWireup="true"

 protected void Button1_Click(object sender, EventArgs e)

    {

         if (GVResult.Rows.Count + 1 < 65536)

         {

             StringWriter tw = new StringWriter();

             System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

             HtmlForm frm = new HtmlForm();

             Response.Charset = "utf-8";         

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

             Response.AddHeader("content-disposition", "attachment;filename=" + System.DateTime.Now.ToString() + ".xls");

             GVResult.RenderControl(hw);

             Response.Write(tw.ToString());

             Response.End();            

         }

        else

            Response.Write("Too many rows - Export to Excel not possible(65536)");

       

    }