GridView匯出Excel的超好樣例

2021-09-06 18:54:53 字數 1720 閱讀 1115

事實上網上有非常多關於excel的樣例,可是不是非常好,他們的**沒有非常全,讀的起來還非常晦澀。經過這幾天的摸索,最終能夠完畢我想要匯出報表excel的效果了。以下是我的效果圖。

一.前台的頁面圖

gridview的第一頁的內容

gridview第二頁的內容:

大家可能遇到這樣的情況,就是這個時候匯出excel的時候,開啟excel的時候發現gridview的第二頁的內容卻沒有匯出到excel表裡面。事實上解決這樣的情況,非常easy,僅僅要在匯出之前,把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)

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的button事件裡面記得先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 之類的資料。如果不...