GridView匯出Excel的超好例子

2021-08-06 07:21:49 字數 1952 閱讀 2846

**:

其實網上有很多關於excel的例子,但是不是很好,他們的**沒有很全,讀的起來還很晦澀。經過這幾天的摸索,終於可以完成我想要匯出報表excel的效果了。下面是我的效果圖。

一.前台的頁面圖

gridview的第一頁的內容

gridview第二頁的內容:

大家可能遇到這種情況,就是這個時候匯出excel的時候,開啟excel的時候發現gridview的第二頁的內容卻沒有匯出到excel表裡面。其實解決這種情況,很簡單,只要在匯出之前,把gridview的設定分頁設定為flase就行了。

下面是我匯出gridview裡面的全部內容,開啟excel表如下:

這就可以匯出全部的gridview裡面的內容了,包括了gridview的第一頁和第二頁的內容。

二、實現的**

1.前台的**:

'>

'>

'>

'>

2.後台**如下:

using system;

using system.collections;

using system.configuration;

using system.data;

using system.linq;

using system.web;

using system.web.security;

using system.web.ui;

using system.web.ui.htmlcontrols;

using system.web.ui.webcontrols;

using system.web.ui.webcontrols.webparts;

using system.xml.linq;

using model;

using dal;

using system.collections.generic;

using system.io;

public partial class _3c_callmanager_callrecord : system.web.ui.page

}public string getresult(string str)

public void bind()

protected void btnexcel_click(object sender, eventargs e)

///

/// 將網格資料匯出到excel

///

/// 網格名稱(如gridview1)

/// 要儲存的檔名

///

public override void verifyrenderinginserverform(control control)

protected void gvrecord_prerender(object sender, eventargs e)

protected void gvrecord_pageindexchanged(object sender, eventargs e)

protected void gvrecord_pageindexchanging(object sender, gridviewpageeventargs e)

}注意問題說明:

1)前台頁面注意新增enableeventvalidation="false",要不然會報錯的。

2)gridview的繫結資料庫的**

this.gvrecord.pageindex = e.newpageindex;  //注意這個不能少。

bind();

4)在匯出excel的按鈕事件裡面記得先gvrecord.allowpaging = false;把gridview的分頁取消之後,再呼叫gridviewtoexcel方法。

5)下面的不可少。

public override void verifyrenderinginserverform(control control)

gridview 匯出excel 總結

protected void button1 click object sender,eventargs e private void export string filetype,string filename 如果沒有下面方法會報錯型別 gridview 的控制項 gridview1 必須放在具...

gridview 匯出excel 總結

private void export string filetype,string filename 如果沒有下面方法會報錯型別 gridview 的控制項 gridview1 必須放在具有 runat server 的窗體標記內 public override void verifyrender...

GridView匯出Excel方法

方法一 將 直接寫在頁面 資料匯出 方法二 將以上 改進成公共方法 將網格資料匯出到excel 網格名稱 如gridview1 要儲存的檔名 三 注意事項 在匯出的時候,如果某個欄位為長數字 如身份證號碼511922198507151512 以0開頭的編號 如0809111212 之類的資料。如果不...