DataGrid control export EXCEL

Source: Internet
Author: User
Private void btnexcel_Click (object sender, System. EventArgs e)

{

This. datagridName. AllowCustomPaging = true;

This. Maid. CurrentPageIndex = 0;

This. Maid. PageSize = 100000;

BinddatagridName ();

// This is the system setting

Response. Clear ();

Response. Buffer = true;

Response. Charset = "GB2312"; // Response. Charset = "UTF-8 ";

String filename = DateTime. Now. ToString (). Replace ("", "-"). Replace (":","-");

Response. AppendHeader ("Content-Disposition", "attachment; filename =" + filename + ". xls"); // FileName.xls is the name of the excel file to be saved.

Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");

// Set the output stream to simplified Chinese

Response. ContentType = "application/ms-excel"; // set the output file type to an excel file.

This. EnableViewState = false;

System. Globalization. CultureInfo myCItrad = new System. Globalization. CultureInfo ("ZH-CN", true );

System. IO. StringWriter oStringWriter = new System. IO. StringWriter (myCItrad );

System. Web. UI. HtmlTextWriter oHtmlTextWriter = new System. Web. UI. HtmlTextWriter (oStringWriter );

// Output html format.

OHtmlTextWriter. Write ("<center> <B> <font size = 4> here </font> </B> <br> ");

OHtmlTextWriter. Write ("<font size = 3> author </font> </center> ");

// Replace the datagridName with the id of your datagrid

This. datagridName. RenderControl (oHtmlTextWriter );

Response. Write (oStringWriter. ToString ());

Response. End ();

This. datagridName. AllowCustomPaging = false;

This. Maid. PageSize = 15;

This. Maid. CurrentPageIndex = 0;

BinddatagridName ();

}

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.