0

I am trying to export data to Excel. And I have GridView with paging enabled. When I download data using HtmlTextWriter it give download only to visible record in GridView. Can we download all the record in single excel file? And also downloaded-data should be with the same sorted state as sorted in GridView?

Client Source code

<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> 

Here is Server Side

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) { } } 
1
  • Yes you can do it but needs to get data again from the database or disable the gridview pagind and refresh Grid. I'ts better to make another method getting the data from the database and then build the excel. Commented Apr 24, 2014 at 9:23

2 Answers 2

3

You may try to do like this

 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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

i already tried this and works perfectly in chrome but not in IE 11
-1

//try this code inside the button

 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 Comment

Welcome to Stack Overflow. Just a suggestion, consider adding an explanation to your answer. 'Code only answers' are frowned upon around here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.