Export a gridview to excel and specify a display Column

Source: Internet
Author: User

There are two methods:
1st method: in the Excel export event, at the beginning, hide the columns that do not need to be output in the gridview (for example, hide the 1st column: abilitylicencegrd. columns [0]. visible = false ;),

2nd Methods: Get data directly from the database rather than from the gridview. You can fetch the database into the able (filter out unwanted columns when getting the data), and then output the data in the datatable to excel in the form of an HTTP stream.

// Export stringwriter as a stream to excel. the Excel file name is exportfilename.
Protected void excelexport (stringwriter SW, string exportfilename)
{
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
// Avoid garbled file names
String filename = httputility. urlencode (exportfilename, encoding. utf8 );
String STR = "attachment; filename =" + filename + ". xls ";

Response. appendheader ("content-disposition", STR );
Response. contenttype = "application/MS-excel ";
Response. Write (SW );
Response. End ();
}

// Assemble the data in the datatable, such as adding a column header (the column header name is displayed in the first row of the Excel file)
Public static stringwriter getstringwriter (datatable DT)
{
Stringwriter Sw = new stringwriter ();

// Write the column header first to ensure that the List header can be output without data
Sw. Write ("student ID" + "\ t ");
Sw. Write ("name" + "\ t ");
Sw. Write (SW. newline );

// If data is included
If (DT! = NULL)
{
// Write data
Foreach (datarow DR in DT. Rows)
{
Sw. Write (Dr ["studentid"]. tostring () + "\ t ");
Sw. Write (Dr ["studentname"]. tostring () + "\ t ");
// Line feed
Sw. Write (SW. newline );
}
}
Sw. Close ();
Return SW;
}

// Export button event on the pageCode
Protected void button#click (Object sender, eventargs E)
{
Datatable dt = obtain the expected data from the database based on the condition (where the unwanted columns are filtered out );
Excelexport (getstringwriter (DT), "exported Excel file name ");
}

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.