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


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/";
                    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

You are viewing reply posted by: Mudassar on Mar 14, 2012 12:19 AM.
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

I agree, here is the link: