This method is the most effective, so post it to share with you
Protected void btnOutExcel_Click (object sender, EventArgs e)
{
If (GridView1.Rows. Count> 0)
{
// Call the export Method
ExportGridViewForUTF8 (GridView1, DateTime. Now. to1_datestring () + ". xls ");
}
Else
{
Obo. Common. MessageBox. Show (this, "No data can be exported. Please query the data first! ");
}
}
/// <Summary>
/// Overload. Otherwise, the "GridView1" control of the "type" GridView "must be placed in the form with runat = server." error
/// </Summary>
/// <Param name = "control"> </param>
Public override void VerifyRenderingInServerForm (Control control Control)
{
// Base. VerifyRenderingInServerForm (control );
}
/// <Summary>
/// Export Method
/// </Summary>
/// <Param name = "GridView"> </param>
/// <Param name = "filename"> name of the saved file </param>
Private void ExportGridViewForUTF8 (GridView, string filename)
{
String attachment = "attachment; filename =" + filename;
Response. ClearContent ();
Response. Buffer = true;
Response. AddHeader ("content-disposition", attachment );
Response. Charset = "UTF-8 ";
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("UTF-8 ");
Response. ContentType = "application/ms-excel ";
System. IO. StringWriter sw = new System. IO. StringWriter ();
HtmlTextWriter htw = new HtmlTextWriter (sw );
GridView. RenderControl (htw );
Response. Output. Write (sw. ToString ());
Response. Flush ();
Response. End ();
}
Protected void btnOutExcel_Click (object sender, EventArgs e)
{
If (GridView1.Rows. Count> 0)
{
// Call the export Method
ExportGridViewForUTF8 (GridView1, DateTime. Now. to1_datestring () + ". xls ");
}
Else
{
Obo. Common. MessageBox. Show (this, "No data can be exported. Please query the data first! ");
}
}
/// <Summary>
/// Overload. Otherwise, the "GridView1" control of the "type" GridView "must be placed in the form with runat = server." error
/// </Summary>
/// <Param name = "control"> </param>
Public override void VerifyRenderingInServerForm (Control control Control)
{
// Base. VerifyRenderingInServerForm (control );
}
/// <Summary>
/// Export Method
/// </Summary>
/// <Param name = "GridView"> </param>
/// <Param name = "filename"> name of the saved file </param>
Private void ExportGridViewForUTF8 (GridView, string filename)
{
String attachment = "attachment; filename =" + filename;
Response. ClearContent ();
Response. Buffer = true;
Response. AddHeader ("content-disposition", attachment );
Response. Charset = "UTF-8 ";
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("UTF-8 ");
Response. ContentType = "application/ms-excel ";
System. IO. StringWriter sw = new System. IO. StringWriter ();
HtmlTextWriter htw = new HtmlTextWriter (sw );
GridView. RenderControl (htw );
Response. Output. Write (sw. ToString ());
Response. Flush ();
Response. End ();
} If the error message "RegisterForEventValidation can be called only during Render () Execution" appears.
There are two ways to solve the above problem:
1. modify web. config (not recommended) <pages enableEventValidation = "false"> </pages>
2. Modify it directly on the export Execl page
<% @ Page Language = "C #" EnableEventValidation = "false" AutoEventWireup = "true" CodeFile = "ExportGridView. aspx. cs" Inherits = "ExportGridView" %>
From Qiu zhengxiang's column