Quick way to export Excel from GridView in ASP. NET

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.