views:

65

answers:

3

i have a code for export to excel,in my gridview i set paging to dispaly no of records in pagecount. but in my export to excel it is not giving me entire records,insted it is showing me the same paging with 6 record.

my code:

string attachment = "attachment; filename=Contacts.xls";

        Response.ClearContent();

        Response.AddHeader("content-disposition", attachment);

        Response.ContentType = "application/ms-excel";



        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);



        // Create a form to contain the grid

        HtmlForm frm = new HtmlForm();



        GrdDynamicControls.AllowPaging = false;
        GrdDynamicControls.Parent.Controls.Add(frm);

        frm.Attributes["runat"] = "server";

        frm.Controls.Add(GrdDynamicControls);



        frm.RenderControl(htw);

        //GridView1.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();

how to change /disable the paging to get all the records from my gridview

i have numbers in gridview as +9199.... but after exporting it is showing me in the format of 9.99....

how to pass the formatcells in numbers from here..

+1  A: 

I'm not familiar with ASP.Net, but I beleive that you bind some datasource (dataTable or Lis<>) to your grid, why don't you export data source instead of dataGrid?

Orsol
A: 

You can export gridview datasource (e.g. dataset) to excel.
Here is a sample code for doing this. You can refer to this link for more information.

using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Whatever
{
 /// 
 /// This class provides a method to write a dataset to the HttpResponse as
 /// an excel file. 
 /// 
 public class ExcelExport
 {
  public static void ExportDataSetToExcel(DataSet ds, string filename)
  {
   HttpResponse response = HttpContext.Current.Response;

   // first let's clean up the response.object
   response.Clear();
   response.Charset = "";

   // set the response mime type for excel
   response.ContentType = "application/vnd.ms-excel";
   response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

   // create a string writer
   using (StringWriter sw = new StringWriter())
   {
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
     // instantiate a datagrid
     DataGrid dg = new DataGrid();
     dg.DataSource = ds.Tables[0];
     dg.DataBind();
     dg.RenderControl(htw);
     response.Write(sw.ToString());
     response.End(); 
    }
   }
  }
 }
}
Ahmed
A: 

I believe you would need to rebind your grid after disabling paging to get all your records.

I'd also echo Orsol's comment; where instead of exporting what's in the grid; export the datasource.

Jim B