Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 9-Mar-11 0:39am
Raj.rcr1.6K
Edited 9-Mar-11 18:25pm
v3
Comments
Sandeep Mewara at 9-Mar-11 6:44am
   
Did you try to Google it?
Raj.rcr at 9-Mar-11 7:03am
   
I tried.. but not getting the solution as I need...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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


HTH
Rajeev


Please vote and mark the answer as accepted if this helps
  Permalink  
Comments
Raj.rcr at 9-Mar-11 13:45pm
   
This is not working.. I had tried the same before posting this question over here..
rajivpande86 at 9-Mar-11 15:48pm
   
And did you also change the filename extension?
Raj.rcr at 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..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
Raj.rcr at 9-Mar-11 14:00pm
   
hi, you have given the link for 2003 format. I want it for 2007 format..
E$w@r at 9-Mar-11 14:06pm
   
Try this free one
http://www.gemboxsoftware.com/GBSpreadsheetFree.htm
E$w@r at 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 at 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)

  Print Answers RSS
0 OriginalGriff 328
1 Gihan Liyanage 292
2 Sergey Alexandrovich Kryukov 275
3 ClimerChinna 222
4 Abhinav S 187
0 Sergey Alexandrovich Kryukov 8,213
1 OriginalGriff 7,089
2 CPallini 2,598
3 Richard MacCutchan 1,925
4 Abhinav S 1,778


Advertise | Privacy | Mobile
Web04 | 2.8.140826.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100