2016-03-06 23 views
3

我在我的C#asp.net窗體應用程序中使用了gridview,並且啓用了「AllowPaging」,現在只要我想將其轉換爲Excel,只顯示第一頁。轉換使用「HtmlTextWriter」和「StringWriter」完成。如何將多個頁面的gridview數據導出到excel文檔中?

- 預先感謝。

+0

你能告訴我們你正在使用的代碼嗎? – jomsk1e

+0

感謝您的關注@jomsk 1e,但是Md。Tahmedul Abedin的回答幫助我達成了解決方案。 –

回答

1

要匯出的GridView Excel您可以使用在你的項目中學習下課。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Web.UI.WebControls; 
using System.Web.UI; 
using System.Web; 

namespace gride_edit 
{ 
    public class GridToExcel 
    { 
     public static void ExportToExcel(string strFileName, GridView gv) 
     { 
      using (StringWriter sw = new StringWriter()) 
      { 
       using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
       { 

        // Create a form to contain the grid 
        Table table = new Table(); 

        // add the header row to the table 
        if (gv.HeaderRow != null) 
        { 
         PrepareControlForExport(gv.HeaderRow); 
         table.Rows.Add(gv.HeaderRow); 
        } 

        // add each of the data rows to the table 
        foreach (GridViewRow row in gv.Rows) 
        { 
         PrepareControlForExport(row); 
         row.Style["background-color"] = "#FFFBD6"; 
         table.Rows.Add(row); 
        } 

        // add the footer row to the table 
        if (gv.FooterRow != null) 
        { 
         PrepareControlForExport(gv.FooterRow); 
         table.Rows.Add(gv.FooterRow); 
        } 

        // render the table into the htmlwriter 
        table.RenderControl(htw); 

        HttpContext.Current.Response.Clear(); 
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", strFileName)); 
        HttpContext.Current.Response.ContentType = "application/ms-excel"; 
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 

        //render the htmlwriter into the response 
        HttpContext.Current.Response.Write(sw.ToString()); 
        HttpContext.Current.Response.End(); 
       } 
      } 
     } 

     private static void PrepareControlForExport(Control control) 
     { 
      for (int i = 0; i < control.Controls.Count; i++) 
      { 
       Control current = control.Controls[i]; 
       if (current is LinkButton) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 
       } 
       else if (current is ImageButton) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 
       } 
       else if (current is HyperLink) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 
       } 
       else if (current is DropDownList) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 
       } 
       else if (current is HiddenField) 
       { 
        control.Controls.Remove(current); 
       } 
       else if (current is CheckBox) 
       { 
        control.Controls.Remove(current); 
        control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 
       } 

       if (current.HasControls()) 
       { 
        PrepareControlForExport(current); 
       } 
      } 
     } 
    } 
} 

並且可以調用這個函數來導出。

GridToExcel.ExportToExcel("Report" + DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + ".xls", GridView1); 

和出口的一個Excel的多個分頁的GridView,調用轉換功能「ExportToExcel()」之前只是用這些線。

GridView1.AllowPaging = false; 
bindGridView(); 

我從網上獲得了「GridToExcel」類。它爲我工作,希望它也能爲你工作。請告訴我。

+0

非常感謝。這真的很簡單,我沒有注意到AllowPaging可以解決這個問題。 –

0

一般來說,你可以使用下面的函數

public void Export(HttpResponseBase Response, object data) 
{ 
     var gridItem = new System.Web.UI.WebControls.GridView(); 
     gridItem.DataSource = data; 
     gridItem.DataBind(); 
     StringWriter writer = new StringWriter(); 
     HtmlTextWriter html = new HtmlTextWriter(writer); 

     Response.ClearContent(); 
     Response.AddHeader("content-disposition", "attachment; filename=result.xls"); 
     Response.ContentType = "application/excel"; 

     gridItem.RenderControl(html); 
     Response.Write(writer.ToString()); 
     Response.End(); 
} 

,你可以調用這個函數作爲

var details = /* your query result goes here */ ; 
Export(Response , details); 

希望它可以幫助你

+0

我以不同的方式完成了這項工作,但您的工作還是有效的,但是@Md的下一個答案。 Tahmidul Abedin解決了gridview多頁面轉換問題。這真的是一個簡單的....非常感謝您的幫助。 –

相關問題