Some of the code on the web is basically similar to the principle, there is also a problem, is:
The control "Ctl00_center_gridview1" of type "GridView" must be placed inside a form tag that has a runat=server. Note: An unhandled exception occurred during the execution of the current WEB request. Check the stack trace for more information about the error and where the error occurred in the code. Exception Details: System.Web.HttpException: The control "ctl00_center_gridview1" of the type "GridView" must be placed inside a form tag with Runat=server.
The error description is that I'm commenting that this procedure is a false quote,
Copy Code code as follows:
Publicoverridevoidverifyrenderinginserverform (Controlcontrol)
//{
Base. Verifyrenderinginserverform (Control);
//}
Although the contents of this method are also commented, that is, this is an empty method, but if there is no method, the program will report the above error. The first time you see this error is to think of a previous error in Ajax program times is very similar. The same is the result of not rewriting the Verifyrenderinginserverform method. In this reminder to use the friend note, the following posted code exported to Excel
Copy Code code as follows:
Usingsystem;
Usingsystem.data;
Usingsystem.configuration;
Usingsystem.collections;
Usingsystem.web;
usingSystem.Web.Security;
UsingSystem.Web.UI;
UsingSystem.Web.UI.WebControls;
UsingSystem.Web.UI.WebControls.WebParts;
UsingSystem.Web.UI.HtmlControls;
Usingsystem.io;
<summary>
Summary description of Toexclehelper
</summary>
Publicclassexporthelper
{
Publicstaticvoidexporttoexcel (Ilistdatalist,string[]fields,string[]headtexts,stringtitle)
{
Gridviewgvw=newgridview ();
Intcolcount,i;
Export only the specified field if the filtered field corresponds to the number of column header names
if (fields. Length!=0&&fields. Length==headtexts.length)
{
Colcount=fields. Length;
Gvw. Autogeneratecolumns=false;
for (i=0;i<colcount;i++)
{
Boundfieldbf=newboundfield ();
Bf. Datafield=fields[i];
Bf. Headertext=headtexts[i];
Gvw. Columns.Add (BF);
}
}
Else
{
Gvw. Autogeneratecolumns=true;
}
Setstype (GVW);
Gvw. Datasource=datalist;
Gvw. DataBind ();
exportToExcel (Gvw,title);
}
<summary>
Export Data to Excel
</summary>
<paramname= "DataList" >IListData</param>
<paramname= "Fields" > Fields to Export </param>
<paramname= "Headname" > field corresponds to the display name </param>
Publicstaticvoidexporttoexcel (ilistdatalist,string[]fields,string[]headtexts)
{
exportToExcel (datalist,fields,headtexts,string. Empty);
}
<summary>
Set style
</summary>
<paramname= "GVW" ></param>
Privatestaticvoidsetstype (GRIDVIEWGVW)
{
Gvw. Font.name= "Verdana";
Gvw. Borderstyle=system.web.ui.webcontrols.borderstyle.solid;
Gvw. Headerstyle.backcolor=system.drawing.color.lightcyan;
Gvw. Headerstyle.forecolor=system.drawing.color.black;
Gvw. Headerstyle.horizontalalign=system.web.ui.webcontrols.horizontalalign.center;
Gvw. Headerstyle.wrap=false;
Gvw. Headerstyle.font.bold=true;
Gvw. headerstyle.font.size=10;
Gvw. rowstyle.font.size=10;
}
<summary>
Export data from the GridView to Excel
</summary>
<paramname= "GVW" ></param>
<paramname= "DataList" ></param>
Publicstaticvoidexporttoexcel (Gridviewgvw,stringtitle)
{
Stringfilename;
Httpcontext.current.response.buffer=true;
HttpContext.Current.Response.ClearContent ();
HttpContext.Current.Response.ClearHeaders ();
Filename=string. Format ("Xhmd{0:yymmddhhmm}.xls", DateTime.Now);
HttpContext.Current.Response.AppendHeader ("Content-disposition", "attachment;filename=" +filename);
Httpcontext.current.response.contenttype= "Application/vnd.ms-excel";
Stringwritertw=newsystem.io.stringwriter ();
Htmltextwriterhw=newsystem.web.ui.htmltextwriter (TW);
Gvw. RenderControl (HW);
if (!string. IsNullOrEmpty (title))
{
HttpContext.Current.Response.Write ("<b><center><fontsize=3face=verdanacolor= #0000FF >" +title+ "</font></center></b>");
}
HttpContext.Current.Response.Write (TW. ToString ());
HttpContext.Current.Response.Flush ();
HttpContext.Current.Response.Close ();
HttpContext.Current.Response.End ();
Gvw. Dispose ();
tw. Dispose ();
Hw. Dispose ();
Gvw=null;
Tw=null;
Hw=null;
}
Publicstaticvoiddatatable2excel (System.Data.DataTabledtData)
{
System.web.ui.webcontrols.datagriddgexport=null;
Current Conversation
System.web.httpcontextcurcontext=system.web.httpcontext.current;
IO is used to export and return Excel files
System.io.stringwriterstrwriter=null;
System.web.ui.htmltextwriterhtmlwriter=null;
if (dtdata!=null)
{
Set encoding and attachment formats
Curcontext.response.contenttype= "Application/vnd.ms-excel";
Curcontext.response.contentencoding=system.text.encoding.utf8;
Curcontext.response.charset= "";
Export Excel Files
Strwriter=newsystem.io.stringwriter ();
Htmlwriter=newsystem.web.ui.htmltextwriter (Strwriter);
To address possible paging in dgdata, you need to redefine a DataGrid that has no paging
Dgexport=newsystem.web.ui.webcontrols.datagrid ();
Dgexport.datasource=dtdata.defaultview;
Dgexport.allowpaging=false;
Dgexport.databind ();
Back to Client
Dgexport.rendercontrol (HTMLWriter);
CurContext.Response.Write (Strwriter.tostring ());
CurContext.Response.End ();
}
}
}