How to fix file format and extension don't match?

Just change the .xls to .xlsx if you have the latest office installed.


I know this problem may be resolved by now, but just trying to help you without modifying the code can still use .xls format in your's and suppress this warning while opening the file by setting a registry. Open reg edit, navigate to HKEY_CURRENT_USER\Software\Microsoft\Office\14\Excel\Security

Create a DWord with name ExtensionHardening and set the value to 0.

This might get your system vulnerable, but it is not a big deal when working in organisation network, at-least when you're sure of downloading the type of doc and source.


The file extension .xls and .xlsx file contain different-different layout. the extension .xls use in version 2003 whereas then version .xlsx extension to be used.
You must export excel file to .xlsx format. It will support in all version as i used.

Add below DLLS into bin folder
1. ClosedXML.dll
2. DocumentFormat.OpenXml.dll

Code to Export to .xlsx

    DataTable dt = new DataTable(); 
    //Create column and inser rows
    using (XLWorkbook wb = new XLWorkbook())
    {         
                var ws = wb.Worksheets.Add(dt, Sheetname);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + p_FileName + ".xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                }
     }

Tags:

C#

Excel