將GridView中的數據導出到Excel / Word中

      在C#程序設計時,咱們很經常使用到GridView控件來顯示數據,那咱們怎樣才能將excel中的數據導到Excel或者Word中呢?
前提條件:你的GridView已經能正常顯示數據了,無論你是用代碼實現的,仍是直接綁定數據庫實現的。
一、若是你的GridView啓動了分頁功能,則要先將該功能關閉,即將allowpaging的屬性設爲false, AllowPaging="false";而後從新調用databind()或者你本身定義的顯示函數,確保全部的數據都顯示在GridView中,再導完數據以後,記得把allowpaging的屬性值改回來。
二、在頁面中添加一個"導出"按鈕,當點擊該按鈕時執行導出動做。雙擊改按鈕,編寫事件處理函數,添加代碼以下:在用到StringWriter類的時候,要在。aspx.cs文件的頭部添加命名空間:using System.IO;html

protected void Button1_Click(object sender, EventArgs e)
{       //導出到Excel中(.xls文件)
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=MyExcel2.xls");
        /*
          
導出時會彈出對話框讓你確認保存位置,默認的文件名爲MyExcel2.xls,
          
你能夠選擇保存,也能夠直接打開
     
  */
        Response.ContentType = "application/excel";
       
 /*
         
若是想將文件導出到word中,則將上面的"MyExcel2.xsl"改爲"MyWord.doc",
         
"application/excel"改爲"application/word"
       */

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        GridView1.AllowPaging = true;
}web

三、重寫VerifyRenderingInServerForm 函數,這樣才能確保運行時爲指定的ASP.NET 服務器控件呈現HtmlForm 控件;函數的內容能夠爲空:sql

public override void VerifyRenderingInServerForm(Control control)
{數據庫

}服務器

四、當出現"只能在執行Render() 的過程當中調用 RegisterForEventValidation;"錯誤時,參考下面網址:http://www.ezloo.com/2008/10/render_registerforeventvalidation.html 解決問題app

如下是一個比較完整的實現"將GridView中的數據導出到Word中"的代碼:ide

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;函數

public partial class GridView_Export_Excel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        string Constr="server=localhost; uid=test;pwd=test;database=test";
        string sqlstr="select * from my_test";
        SqlConnection con = new SqlConnection(Constr);
        SqlDataAdapter ad = new SqlDataAdapter(sqlstr, con);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=MyExcel.doc");
        Response.ContentType = "application/word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        BindData();
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        GridView1.AllowPaging = true;
    }
    public override void VerifyRenderingInServerForm(Control control)
    {ui

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}spa

相關文章
相關標籤/搜索