asp.net to implement Gradview binding database data and Export Excel method _ Practical Tips

Source: Internet
Author: User

This article describes the method that ASP.net implements Gradview bound database data and exports Excel. Share to everyone for your reference, specific as follows:

protected void Showdata_click (object sender, EventArgs e) {SqlConnection myconnection = new SqlConnection ("Data Sou Rce=localhost;initial catalog=test;
  User Id=sa;password=sa ");
  SqlDataAdapter ad = new SqlDataAdapter ("select * from Booklist", MyConnection);
  DataSet ds = new DataSet (); Ad.
  Fill (DS);
  This.gvShowData.DataSource = ds;
This.gvShowData.DataBind ();
  }//Export Excel table protected void Btnexporttoexcel_click (object sender, EventArgs e) {Response.Charset = "GB2312";
  response.contentencoding = System.Text.Encoding.GetEncoding ("GB2312");
  Response.AddHeader ("Content-type", "application/vnd.ms-excel");
  Response.AddHeader ("Content-disposition", "Myexcelfile.xls");
  Export in this encoding mode will not appear garbled stringwriter sw = new StringWriter ();
  HtmlTextWriter HTW = new HtmlTextWriter (SW);
  Gvshowdata.rendercontrol (HTW); Response.Write (SW.
  ToString ());
Response.End ();
//Be sure to write, or make an error!!

 public override void Verifyrenderinginserverform {}

I hope this article will help you with ASP.net programming.

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.