Click here to Skip to main content
15,884,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have the below code to export the data into excel 2003 format:

protected void lnkbtnExport_Click(object sender, EventArgs e)
    {
        ExcelHead.Visible = true;     
        string fileName = "attachment;filename= UserProdReport_" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xls";
        Response.Clear();
        Response.AddHeader("content-disposition", fileName);
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        ExcelHead.RenderControl(htmlWrite);    
        gvUserProd.RenderControl(htmlWrite);      
        Response.Write(stringWrite);     
        Response.End();
        ExcelHead.Visible = false;
    }   


Now I want to export the same into excel 2007 format.. Please anyone give me an idea.
Regards,
Posted
Updated 9-Mar-11 18:25pm
v3
Comments
Sandeep Mewara 9-Mar-11 6:44am    
Did you try to Google it?
Raj.rcr 9-Mar-11 7:03am    
I tried.. but not getting the solution as I need...

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




HTH
Rajeev


Please vote and mark the answer as accepted if this helps
 
Share this answer
 
Comments
Raj.rcr 9-Mar-11 13:45pm    
This is not working.. I had tried the same before posting this question over here..
rajivpande86 9-Mar-11 15:48pm    
And did you also change the filename extension?
Raj.rcr 10-Mar-11 0:18am    
Yes, for the first time I had changed with this contrnt type.. It didnt work. Then I removed the extension..
For .xlsx discussions:
http://forums.asp.net/t/1221467.aspx/1?Export+GridView+to+Excel+xlsx+Excel+2007+Format+[^]

For .xls you get more than enough here
 
Share this answer
 
 
Share this answer
 
Comments
Raj.rcr 9-Mar-11 14:00pm    
hi, you have given the link for 2003 format. I want it for 2007 format..
TweakBird 9-Mar-11 14:06pm    
Try this free one
http://www.gemboxsoftware.com/GBSpreadsheetFree.htm
TweakBird 9-Mar-11 14:18pm    
Change FileName, Header & Content Type like Here,
string fileName = "attachment;filename= UserProdReport_" + DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") + ".xlsx";

Response.AddHeader("content-disposition","attachment;filename="+ fileName);

Response.ContentType = "application/vnd.xlsx";

Try this way.
Raj.rcr 10-Mar-11 0:24am    
I tried this also. An error says:
"Excel cannot open the file 'UserProdReport_10-03-2011-10-24-44.xlsx' because the file
format or file extension is not valid. Verify that the file has not been
corrupted and that the file extension matches the format of the file.

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