ASP.NET 2.0中GridView 导出到Office时出错的解决方法
Vs2003中我們常用DataGrid導出到Excel的方法如下:
Server Error in '/DataMeasure' Application.
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();
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:
|
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();
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);
}
{
//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();
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)中.