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)中.