ASP.Net exporting to excel (XLSX Excel 2007 format)

Last Reply on Mar 14, 2012 12:19 AM By Mudassar

Posted on Mar 13, 2012 12:55 AM

in my web application page i have a option to export to excel . I am able to export the data to Excel but have 2 issues

 

1)  i hav ethe option of exporting in .XLS or .XLSX format

i am able to export in .xls format from any system ie the system having office 2007 or 2003

 

but when i am trying to export in .XLSX format i get error

Microsoft Office Excel
---------------------------
The file you are trying to open, 'noup.xlsx', is in a different format than specified by the file extension. Verify that the file is not corrupted and that the file extension matches the file format
---------------------------
Ok
---------------------------

 

2) when exporting i want the excel sheet to be saved with the name thats selected from a dropdown

suppose the dropdown has text selected as CITY

i want the excel sheet as CITY.XLS/.XLSX

i my developemnet system it works fine with below code

 string filename = dropdown1.SelectedItem.Text
 if (Extension.SelectedValue.ToString() == ".xls")
                {
                    Page.Response.ContentType = "application/vnd.ms-excel";
                    ext = ".xls";
                }
                else if (Extension.SelectedValue.ToString() == ".xlsx")
                {
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    ext = ".xlsx";
                }



  Response.AddHeader("content-disposition", "attachment; filename=" + filename + ext);
                System.IO.Stream sm = Page.Response.OutputStream;


but on production server i get the name of .aspx page

Results 1 - 5 of 13 123
Posted on Mar 14, 2012 12:19 AM

You can export datatable ot dataset to Excel using openxml to Excel 2007 format.

Complete sample code http://lateral8.com/articles/2010/3/5/openxml-sdk-20-export-a-datatable-to-excel.aspx

Posted on Mar 13, 2012 11:30 PM

to further clarification about exporting data toexcel 2007

using the above method when i fire the export code on server having office 2007 ie using localhost

i get a mesage window having 3 buttons Open, save & help

1) if i click Open i get another message window asking to open in any format  with buttons YES , NO

if i click YES the excel file opens with data

2) if i click save i get the save dialog box & i can save the file & has extension .xlsx

after saving when i try to open the file i get the message with only OK button mentioned

just for info if needed

 

any sample to create a 2007 Excel file on server using OpenXML and then download it

 

 

 

 

Posted on Mar 13, 2012 07:33 AM

Actually you cannot export to xlsx using this way of exporting. You need to first create a 2007 Excel file on server using OpenXML and then download it

Posted on Mar 13, 2012 04:04 AM

xls get opened from everywhere

no problem exporting to  xls

Posted on Mar 13, 2012 04:01 AM

Does this happen with xls also?