這篇文章主要介紹了asp.net實(shí)現(xiàn)Gradview綁定數(shù)據(jù)庫(kù)數(shù)據(jù)并導(dǎo)出Excel的方法,涉及asp.net操作Gradview實(shí)現(xiàn)數(shù)據(jù)庫(kù)綁定及數(shù)據(jù)導(dǎo)出的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
本文實(shí)例講述了asp.net實(shí)現(xiàn)Gradview綁定數(shù)據(jù)庫(kù)數(shù)據(jù)并導(dǎo)出Excel的方法。分享給大家供大家參考,具體如下:
- 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();
- }
- //導(dǎo)出Excel表
- 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");
- //以此編碼模式導(dǎo)出才不會(huì)出現(xiàn)亂碼
- StringWriter sw = new StringWriter();
- HtmlTextWriter htw = new HtmlTextWriter(sw);
- gvShowData.RenderControl(htw);
- Response.Write(sw.ToString());
- Response.End();
- }
- //一定要寫,否則出錯(cuò)!!
- public override void VerifyRenderingInServerForm(Control control)
- {
- }
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選