How to export Excel using GridView

Source: Internet
Author: User

In order to complete the tasks of the leadership, data is being presented over the past few days. because the time is tight, it is not too complicated. The GridView is used to display database tables. Almost no settings are made for the GridView format. Load the SQL statement from the configuration file and bind the data to the GridView directly when it runs out. Some problems have been found. For example, the width of automatically bound columns in the GridView cannot be set, and the table output in the GridView does not contain the width information, therefore, when many table columns are displayed, it is difficult to squeeze into the page. because the number of columns in the table is not fixed, it is impossible to simply use the template column, finally, we had to directly set the table width to a large number.

In addition, an Excel export function is provided. The main code is as follows::

Copy codeThe Code is as follows:
Private void DumpExcel (GridView gv, string FileName)
{// Export with a format
String style = @ "<style>. text {mso-number-format :\@;}</style> ";
Response. ClearContent ();
Response. Charset = "GB2312 ";
Response. ContentEncoding = System. Text. Encoding. UTF8;
Response. AddHeader ("content-disposition", "attachment; filename =" + HttpUtility. UrlEncode (FileName, Encoding. UTF8). ToString ());
Response. ContentType = "application/excel ";
StringWriter sw = new StringWriter ();
HtmlTextWriter htw = new HtmlTextWriter (sw );
Gv. RenderControl (htw );
// Style is added dynamically
Response. Write (style );
Response. Write (sw. ToString ());
Response. End ();
}
Public override void VerifyRenderingInServerForm (Control control Control)
{
}

The overload function of Row 17 above is required. Otherwise, the error "The GridView must be in the From body of run = server" will be reported.
In addition, the function of the variable style is to control the style of the GridView column, so as to avoid the problem that leading 0 character in the excel table is taken away as a number, through Response. add the Write Method to the output stream. Add the style to the ID column. This step needs to be completed in the RowDataBound event:
Copy codeThe Code is as follows:
1 protected void gvUsers_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. DataRow)
{
E. Row. Cells [1]. Attributes. Add ("class", "text ");
}
}

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.