Export data from a ASP.net page to Excel or Word

Source: Internet
Author: User
asp.net|excel|word| Data | page

Implemented in the following button click events:
private void Btnmime_click (object sender, System.EventArgs e)
{
Binddata ();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader ("Content-disposition", "inline;filename=")
+ httputility.urlencode ("Download file. xls", Encoding.UTF8));

If the output is word, modify to the following code
Response.ContentType = "Application/ms-word"
Response.AddHeader ("Content-disposition", "Inline;filename=test.doc")
StringBuilder sb=new StringBuilder ();
System.IO.StringWriter SW = new System.IO.StringWriter (SB);
System.Web.UI.HtmlTextWriter HW = new System.Web.UI.HtmlTextWriter (SW);
Sb. Append ("Dgshow.rendercontrol (HW);
Sb. Append ("</body>Response.Write (sb.) ToString ());
Response.End ();
}
Note: 1. If there is a button column in the DataGrid, it should be hidden before exporting.
2. If the DataGrid has pagination, but also print all the data, you should first cancel the paging.



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.