Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
I searched on google.

i want to export data to excel 2007(xlsx) format as it is having 80,000 records.


C#
GridView GridView1 = new GridView();
                GridView1.DataSource = ((DataTable)Session["EXECUTIVE_ACT_RPT"]);
                GridView1.DataBind();
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("Content-Disposition", "attachment; filename=abc.xlsx");
                //Choose one of these: as the contentType
                //Excel 2003 : "application/vnd.ms-excel"
                //Excel 2007 : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 

                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

                System.IO.StringWriter writer = new System.IO.StringWriter();

                System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
                GridView1.RenderControl(html);

                Response.Write(writer);

                Response.End();


when i open the file it shows that the file is not valid.
it says :
"either corrupted or the extension is not valid"

i dont want to export it in xls format.
Posted

 
Share this answer
 
v2
Comments
Prasad_Kulkarni 28-Feb-13 7:46am    
Link formatted.
vaibhav mahajan 28-Feb-13 8:37am    
i tried to convert in csv.
but it is not converting

Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.csv"));
// Response.ContentType = "text/csv";
Please follow the link below

Export Gridview Data to Excel in ASP.NET[^]
Thanks
 
Share this answer
 
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xlsx"));
Response.ContentType = "application/vnd.xls"
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
//Change the Header Row back to white color
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
{
gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in gvdetails.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gvdetails.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}


please try this may be help you....
 
Share this answer
 
Comments
Member 8909786 1-May-13 2:55am    
this code is working but with xls file format
Aroon Kumar 11-Nov-19 6:34am    
Xlsx is not working only xls is working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900