如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel
來源:易賢網(wǎng) 閱讀:610 次 日期:2014-08-28 16:29:56
溫馨提示:易賢網(wǎng)小編為您整理了“如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel,需要的朋友可以參考下

代碼如下:

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ù)庫連接實例

myConnection.Open(); //打開數(shù)據(jù)庫

SqlCommand myCommand = new SqlCommand(sql, myConnection);//創(chuàng)建sql的實例,執(zhí)行一個sql

SqlDataAdapter Adapter = new SqlDataAdapter();//創(chuàng)建一個sql數(shù)據(jù)適配器

Adapter.SelectCommand = myCommand;//屬性設(shè)置為 從數(shù)據(jù)源中檢索記錄

DataSet myDs = new DataSet(); //創(chuàng)建數(shù)據(jù)集實例

Adapter.Fill(myDs);//填充數(shù)據(jù)集

GridView1.DataSource = myDs.Tables[0].DefaultView;//

GridView1.DataBind();

// DataToExcel("測試的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 指定輸出文件的名稱,注意其擴展名和指定文件類型相符,可以為:.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);

//定義一個輸入流

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

//將目標數(shù)據(jù)綁定到輸入流輸出

myGridView.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();

}

//下面這個空語句一定要加,否則會出現(xiàn)“必須放在具有 runat=server 的窗體標記內(nèi)?!钡腻e誤

public override void VerifyRenderingInServerForm(Control control)

{

}

//點擊事件,生成excel

protected void Button1_Click(object sender, EventArgs e)

{

DataToExcel("測試的cxcel", GridView1);

}

}

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)