asp. net Implementation of Gradview Binding Database Data and Exporting Excel
- 2021-07-07 07:15:27
- OfStack
In this paper, an example of asp. net implementation of Gradview binding database data and exporting Excel method. Share it for your reference, as follows:
protected void showData_Click(object sender, EventArgs e)
{
SqlConnection myConnection
= new SqlConnection("Data Source=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 so as not to have garbled codes
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvShowData.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
//1 Be sure to write, or make mistakes! !
public override void VerifyRenderingInServerForm(Control control)
{
}
I hope this article is helpful to everyone's asp. net programming.