2014-04-24 85 views
0

我想導出數據到Excel。我有GridView與分頁啓用。當我使用HtmlTextWriter下載數據時,它只下載到GridView中的可見記錄。我們可以下載單個Excel文件中的所有記錄嗎?並且下載的數據應該和GridView中的排序狀態一樣?導出所有數據到Excel使用GridView與分頁啓用..在asp.net與c#

客戶端源代碼

<asp:GridView ID="gvDetailFleet" runat="server" DataSourceID="SqlDataSource2" 
     AllowPaging="false" AllowSorting="True" AutoGenerateColumns="true" 
     DataKeyNames="OracleId" CellPadding="4" ForeColor="#333333" 
     GridLines="None" > 
     <RowStyle BackColor="#EFF3FB" /> 
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <EditRowStyle BackColor="#2461BF" /> 
     <AlternatingRowStyle BackColor="White" /> 
</asp:GridView> 

這裏是服務器端

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 System.Data.SqlClient; 
using System.IO; 
public partial class FleetReportWithoutVehicle : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!this.IsPostBack) 
     { 
      btnFleetReportWithoutVehicle.Visible = false; 
     } 
    } 

    protected void btnSearch_Click(object sender, EventArgs e) 
    { 
     btnFleetReportWithoutVehicle.Visible = true; 
    } 

    protected void btnExportSalary_Click(object sender, EventArgs e) 
    { 
     Response.ClearContent(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "AttendanceFollowUpReport.xls")); 
     Response.ContentType = "application/ms-excel"; 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter htw = new HtmlTextWriter(sw); 
     gvDetailFleet.AllowPaging = false; 
     gvDetailFleet.HeaderRow.Style.Add("background-color", ""); 
     for (int i = 0; i < gvDetailFleet.HeaderRow.Cells.Count; i++) 
     { 
      gvDetailFleet.HeaderRow.Cells[i].Style.Add("background-color", ""); 
     } 
     gvDetailFleet.RenderControl(htw); 
     Response.Write(sw.ToString()); 
     Response.Flush(); 
     Response.End(); 
    } 

    public override void VerifyRenderingInServerForm(Control control) 
    { 

    } 
} 
+0

是的,你可以做到這一點,但需要從數據庫中再次獲取數據或禁用gridview pagind和刷新網格。我更好的做另一種方法從數據庫中獲取數據,然後構建excel。 –

回答

1

你可以嘗試做這樣的

GridView gv = new GridView(); 
     gv.DataSource = sourceList; //Your datasource from database 
       gv.DataBind(); 
       Response.ClearContent(); 
       Response.Buffer = true; 
       Response.AddHeader("content-disposition", "attachment; filename=Preferred_Funds-" + DateTime.Now.ToShortDateString() + ".xls"); 
       Response.ContentType = "application/ms-excel"; 
       Response.Charset = ""; 
       StringWriter sw = new StringWriter(); 
       HtmlTextWriter htw = new HtmlTextWriter(sw); 
       gv.RenderControl(htw); 
       Response.Output.Write(sw.ToString()); 
       Response.Flush(); 
       Response.End(); 
+0

我已經嘗試過這一點,並且在Chrome瀏覽器中完美工作,但IE瀏覽器不支持11 – Juran

-1

//試試裏面的這段代碼按鈕

Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment;filename=ExportData1.xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/vnd.xls"; 
    StringWriter StringWriter = new System.IO.StringWriter(); 
    HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter); 

    //batigol 
    GridView1.AllowPaging = false; 
    GridView1.DataSource = Session["uu"]; 
    GridView1.DataBind(); 
    // 

    GridView1.RenderControl(HtmlTextWriter); 
    Response.Write(StringWriter.ToString()); 
    Response.End(); 
+1

歡迎使用Stack Overflow。只是一個建議,考慮給你的答案添加一個解釋。 '代碼只是答案'在這裏附近皺起了眉頭。 –

相關問題