ASP.NET 2.0中GridView 匯出到Office時出錯的解決方案

來源:互聯網
上載者:User
Vs2003中我們常用DataGrid導出到Excel的方法如下:

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "BIG5";
        string fileName = "Overview";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("BIG5");
        Response.ContentType = "application/ms-excel";//

        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-TW", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();

用以上方法在VS2005的項目時卻報如下錯誤:

 

Server Error in '/DataMeasure' Application.

Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

Source Error:

Line 244:        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);            Line 245:        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);            Line 246:        GridView1.RenderControl(oHtmlTextWriter);            Line 247:        Response.Write(oStringWriter.ToString());            Line 248:        Response.End();

Source File: d:"SourceCode"DataMeasure"Overview"DataOverview.aspx.cs    Line: 246  

 

有兩種解決方案:

一、在原來代碼的基礎上更改

匯出代碼不變

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "BIG5";
        string fileName = "Overview";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("BIG5");
        Response.ContentType = "application/ms-excel";//

        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-TW", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();

但是要添加如下代碼:

 public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

要注意的是中間的程式碼要注釋掉,這就是忽略檢查在RENDER的時候判斷是否在RUNAT=SERVER中

二、另一種方法

這種方法是建立一個FORM,將GRIDVIEW放入這個FORM中。

 

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        Page page = new Page();
        HtmlForm form = new HtmlForm();
        GridView1.EnableViewState = false;
        page.DesignerInitialize();

        form.Controls.Add(GridView1);
        page.Controls.Add(form);

        page.RenderControl(htw);
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment;filename=Overview.xls");
        Response.Charset = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.Default;
        Response.Write(sb.ToString());
        Response.End();

 

以上兩種方法都可以在VS2005中實現GridView匯出到Office(Excel+Word)中.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.