ASP.NET 2.0でGridViewがOfficeにエクスポートされたときのエラーの解決方法

5315 ワード

Vs 2003でよく使用される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();
以上の方法でVS 2005の項目で次のエラーを報告します.
 

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  
 
2つの解決策があります.
一、元のコードに基づいて変更する
エクスポートコードは変更されません
        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();
 
以上の2つの方法は、VS 2005においてGridViewをOffice(Excel+Word)に導出ことができる.