如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel
更新時(shí)間:2014年02月21日 15:25:38 作者:
這篇文章主要介紹了如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data .SqlClient ;
using System.Data ;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String sqlconn = "Server=.; DataBase=db; Integrated Security=SSPI ";
string sql = "select top 10 * from table";
SqlConnection myConnection = new SqlConnection(sqlconn);// 創(chuàng)建數(shù)據(jù)庫連接實(shí)例
myConnection.Open(); //打開數(shù)據(jù)庫
SqlCommand myCommand = new SqlCommand(sql, myConnection);//創(chuàng)建sql的實(shí)例,執(zhí)行一個(gè)sql
SqlDataAdapter Adapter = new SqlDataAdapter();//創(chuàng)建一個(gè)sql數(shù)據(jù)適配器
Adapter.SelectCommand = myCommand;//屬性設(shè)置為 從數(shù)據(jù)源中檢索記錄
DataSet myDs = new DataSet(); //創(chuàng)建數(shù)據(jù)集實(shí)例
Adapter.Fill(myDs);//填充數(shù)據(jù)集
GridView1.DataSource = myDs.Tables[0].DefaultView;//
GridView1.DataBind();
// DataToExcel("測(cè)試的cxcel", GridView1);
myConnection.Close();//關(guān)閉數(shù)據(jù)庫連接
}
public void DataToExcel(string fileName, GridView myGridView)
{
//定義文檔類型、字符編碼
Response.Clear();
Response.Buffer = false;
//Response.Charset = "utf-8";
Response.Charset = "GB2312";
//下面這行很重要, attachment 參數(shù)表示作為附件下載,您可以改成 online在線打開
//filename=FileFlow.xls 指定輸出文件的名稱,注意其擴(kuò)展名和指定文件類型相符,可以為:.doc || .xls || .txt ||.htm
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件類型 可以為application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他瀏覽器可直接支持文檔
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
//System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
//定義一個(gè)輸入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//將目標(biāo)數(shù)據(jù)綁定到輸入流輸出
myGridView.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
//下面這個(gè)空語句一定要加,否則會(huì)出現(xiàn)“必須放在具有 runat=server 的窗體標(biāo)記內(nèi)?!钡腻e(cuò)誤
public override void VerifyRenderingInServerForm(Control control)
{
}
//點(diǎn)擊事件,生成excel
protected void Button1_Click(object sender, EventArgs e)
{
DataToExcel("測(cè)試的cxcel", GridView1);
}
}
相關(guān)文章
asp.net下Request.QueryString取不到值的解決方法
2008-01-01
asp.net錯(cuò)誤處理Application_Error事件示例
Application_Error事件與Page_Error事件相類似,可使用他捕獲發(fā)生在應(yīng)用程序中的錯(cuò)誤。由于事件發(fā)生在整個(gè)應(yīng)用程序范圍內(nèi),因此您可記錄應(yīng)用程序的錯(cuò)誤信息或處理其他可能發(fā)生的應(yīng)用程序級(jí)別的錯(cuò)誤2014-01-01
ASP.NET Core如何實(shí)現(xiàn)簡(jiǎn)單的靜態(tài)網(wǎng)站滾動(dòng)更新
這篇文章主要給大家介紹了關(guān)于ASP.NET Core如何實(shí)現(xiàn)簡(jiǎn)單的靜態(tài)網(wǎng)站滾動(dòng)更新的相關(guān)資料,文中給出了詳細(xì)實(shí)現(xiàn)的代碼,對(duì)需要的朋友來說很實(shí)用,需要的朋友可以參考下2021-07-07
合并網(wǎng)頁中的多個(gè)script引用實(shí)現(xiàn)思路及代碼
為了更好的進(jìn)行封裝,每個(gè)實(shí)現(xiàn)不同功能的js代碼應(yīng)該有自己的js文件,這樣如果一個(gè)網(wǎng)頁中引用了多個(gè)js文件會(huì)很難管理,所以就出現(xiàn)了合并js這以說,感興趣的朋友不妨參考下本文希望對(duì)你有所幫助2013-02-02
使用ASP.Net?WebAPI構(gòu)建REST服務(wù)
這篇文章介紹了使用ASP.Net?WebAPI構(gòu)建REST服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
設(shè)置DropDownList的當(dāng)前選項(xiàng)
2008-01-01

