asp.net實現(xiàn)Gradview綁定數(shù)據(jù)庫數(shù)據(jù)并導出Excel的方法
更新時間:2015年11月25日 11:51:19 作者:一只小青蛙
這篇文章主要介紹了asp.net實現(xiàn)Gradview綁定數(shù)據(jù)庫數(shù)據(jù)并導出Excel的方法,涉及asp.net操作Gradview實現(xiàn)數(shù)據(jù)庫綁定及數(shù)據(jù)導出的相關技巧,非常簡單實用,需要的朋友可以參考下
本文實例講述了asp.net實現(xiàn)Gradview綁定數(shù)據(jù)庫數(shù)據(jù)并導出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();
}
//導出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");
//以此編碼模式導出才不會出現(xiàn)亂碼
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvShowData.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
//一定要寫,否則出錯??!
public override void VerifyRenderingInServerForm(Control control)
{
}
希望本文所述對大家asp.net程序設計有所幫助。
您可能感興趣的文章:
- asp.net實現(xiàn)數(shù)據(jù)從DataTable導入到Excel文件并創(chuàng)建表的方法
- asp.net頁面中如何獲取Excel表的內容
- 直接在線預覽Word、Excel、TXT文件之ASP.NET
- asp.net中Table生成Excel表格的方法
- asp.net中EXCEL數(shù)據(jù)導入到數(shù)據(jù)庫的方法
- asp.net+ajax+sqlserver自動補全功能實現(xiàn)解析
- asp.net(c#)實現(xiàn)從sqlserver存取二進制圖片的代碼
- 快速插入大量數(shù)據(jù)的asp.net代碼(Sqlserver)
- ASP.NET下向SQLServer2008導入文件實例操作方法
- asp.net實現(xiàn)將Excel中多個sheet數(shù)據(jù)導入到SQLSERVER中的方法
相關文章
Asp.net開發(fā)之webform圖片水印和圖片驗證碼的實現(xiàn)方法
這篇文章主要介紹了Asp.net開發(fā)之webform圖片水印和圖片驗證碼的實現(xiàn)方法,實現(xiàn)思路分為前后臺代碼和效果展示,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-10-10
Silverlight融合ajax實現(xiàn)前后臺數(shù)據(jù)交互
兩年前Silverlight 還未起名,故事發(fā)生在WPF/E 的年代里。07年8月在中軟實習時,我承擔起了在. Net 中嵌入WPF/E 的任務,目的是增強用戶體驗。2009-05-05
剖析ASP.NET MVC的DependencyResolver組件
這篇文章主要為大家剖析ASP.NET MVC的DependencyResolver組件,感興趣的小伙伴們可以參考一下2016-04-04

