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.