ASP. NET method of exporting data to Excel
- 2020-06-15 08:14:49
- OfStack
The principle of a number of codes on the Internet is similar, but there is also a problem:
Control "ctl00_center_GridView1" of type "GridView" must be placed within a form tag with runat=server. Note: An unhandled exception occurred during execution of the current Web request. Check the stack trace for details about the error and the source in the code that caused it. System.Web.HttpException: Control of type "GridView" "ctl00_center_GridView1" must be placed in a form tag with runat=server.
This error description is that I'm annotating this program as an error,
Although the contents of the method are also commented, meaning that it is empty, if there is no method, the program will report the above error. The first time I saw this error was when I thought of a similar error in the ajax program. Again, the VerifyRenderingInServerForm method was not overridden. As a reminder, the code for exporting to Excel is posted below
Control "ctl00_center_GridView1" of type "GridView" must be placed within a form tag with runat=server. Note: An unhandled exception occurred during execution of the current Web request. Check the stack trace for details about the error and the source in the code that caused it. System.Web.HttpException: Control of type "GridView" "ctl00_center_GridView1" must be placed in a form tag with runat=server.
This error description is that I'm annotating this program as an error,
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
// //base.VerifyRenderingInServerForm(control);
//}
Although the contents of the method are also commented, meaning that it is empty, if there is no method, the program will report the above error. The first time I saw this error was when I thought of a similar error in the ajax program. Again, the VerifyRenderingInServerForm method was not overridden. As a reminder, the code for exporting to Excel is posted below
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>
///ToExcleHelper Summary description of
///</summary>
publicclassExportHelper
{
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts,stringtitle)
{
GridViewgvw=newGridView();
intColCount,i;
// Only the specified field is exported if the number of filtered fields is relative to the corresponding 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"> The field to export </param>
///<paramname="HeadName"> The field corresponds to the name displayed </param>
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts)
{
ExportToExcel(dataList,fields,headTexts,string.Empty);
}
///<summary>
/// Set the 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 GridView Data 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;
// The current dialogue
System.Web.HttpContextcurContext=System.Web.HttpContext.Current;
//IO Used to export and return excel file
System.IO.StringWriterstrWriter=null;
System.Web.UI.HtmlTextWriterhtmlWriter=null;
if(dtData!=null)
{
// Set the encoding and attachment format
curContext.Response.ContentType="application/vnd.ms-excel";
curContext.Response.ContentEncoding=System.Text.Encoding.UTF8;
curContext.Response.Charset="";
// export excel file
strWriter=newSystem.IO.StringWriter();
htmlWriter=newSystem.Web.UI.HtmlTextWriter(strWriter);
// In order to solve the dgData There may be a paging situation in which you need to redefine 1 A non-paging one DataGrid
dgExport=newSystem.Web.UI.WebControls.DataGrid();
dgExport.DataSource=dtData.DefaultView;
dgExport.AllowPaging=false;
dgExport.DataBind();
// Return to client
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
}