locked
Excel cannot open the file because the file format or the file extension is not valid RRS feed

  • Question

  • User1744325147 posted

         Hi guys,

         I am using a webForm and trying to upload a xlsx file so that the end user can download it quickly

         The same code worked perfectly with xls file. But once i changed it to xlsx i have got the message above

         Here is my code

         any suggestions?  Thanks

            string attachment = "Attachment; filename=" + ExcelName+".xlsx";  

            page.Response.Clear();

            page.Response.ClearContent();

            page.Response.AddHeader("content-disposition", attachment);

            page.Response.Charset = "";

     

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

     

            page.Response.Charset = "windows-1255";

            page.Response.ContentEncoding = Encoding.GetEncoding("windows-1255");

     

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);  

            HtmlForm frm = new HtmlForm();

            control.Parent.Controls.Add(frm);

            frm.Attributes["runat"] = "server";

            frm.Controls.Add(control);

           control.RenderControl(htw);

            page.Response.Write("<html><head><meta http-equiv='content-type' content='text/html; charset=windows-1255'></head><body>" + sw.ToString() + "</body></html>");

            page.Response.End();

    Monday, July 10, 2017 11:19 AM

Answers

  • User475983607 posted

    This is related to Excel security.  Basically, the file content is HTML but Excel expects valid Excel content not HTML.

    https://social.technet.microsoft.com/Forums/office/en-US/4a15e472-a265-4058-b439-7d79c2670817/xls-file-will-not-open-in-excel-2016-only-gray-view?forum=excel

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 10, 2017 4:05 PM
  • User2053451246 posted

         Hi guys,

         I am using a webForm and trying to upload a xlsx file so that the end user can download it quickly

         The same code worked perfectly with xls file. But once i changed it to xlsx i have got the message above

         Here is my code

         any suggestions?  Thanks

            string attachment = "Attachment; filename=" + ExcelName+".xlsx";  

            page.Response.Clear();

            page.Response.ClearContent();

            page.Response.AddHeader("content-disposition", attachment);

            page.Response.Charset = "";

     

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

     

            page.Response.Charset = "windows-1255";

            page.Response.ContentEncoding = Encoding.GetEncoding("windows-1255");

     

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);  

            HtmlForm frm = new HtmlForm();

            control.Parent.Controls.Add(frm);

            frm.Attributes["runat"] = "server";

            frm.Controls.Add(control);

           control.RenderControl(htw);

            page.Response.Write("<html><head><meta http-equiv='content-type' content='text/html; charset=windows-1255'></head><body>" + sw.ToString() + "</body></html>");

            page.Response.End();

    It bothers me that people still try to export to Excel this way.  You all know the extra overhead is using a GridView.  Pull the same dataset together and use something like NPOI to create the Excel workbook.  Moving from .xls to .xlsx would simply be a matter of changing HSSFWorkbook to XSSFWorkbook and changing the file extension of the downloaded file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 10, 2017 6:36 PM

All replies

  • User-359936451 posted

    There a couple of possible issues. One, if simply change the name of the file from .xls to .xlsx say in windows explorer, the file type is not actually changed, and the upload control does not recognize the file type because it is really .xls and you are telling it that it is .xlsx.

    You can check the file extension in your code with something like this....

     If FileUploadExcel.HasFile Then
                ' Get the size of the file to be uploaded
                Dim fileSize As Long
                fileSize = FileUploadExcel.PostedFile.ContentLength
                ' Get the file name
                Dim fileName As String
                fileName = FileUploadExcel.PostedFile.FileName
                ' Get the extension of the uploaded file.
                Dim extension As String = System.IO.Path.GetExtension(fileName).ToLower
               
                ' Allow only files with .xlsx extensions to be uploaded.
                If extension <> ".xlsx" And extension <> ".xls" Then
                    ' Notify the user why their file was not uploaded.
                    LabelUpload.Text = "Your file was not uploaded because " + _
                                             "it does not have an .xlsx or .xls extension."
                    Exit Sub
                End If

    Monday, July 10, 2017 3:39 PM
  • User475983607 posted

    This is related to Excel security.  Basically, the file content is HTML but Excel expects valid Excel content not HTML.

    https://social.technet.microsoft.com/Forums/office/en-US/4a15e472-a265-4058-b439-7d79c2670817/xls-file-will-not-open-in-excel-2016-only-gray-view?forum=excel

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 10, 2017 4:05 PM
  • User2053451246 posted

         Hi guys,

         I am using a webForm and trying to upload a xlsx file so that the end user can download it quickly

         The same code worked perfectly with xls file. But once i changed it to xlsx i have got the message above

         Here is my code

         any suggestions?  Thanks

            string attachment = "Attachment; filename=" + ExcelName+".xlsx";  

            page.Response.Clear();

            page.Response.ClearContent();

            page.Response.AddHeader("content-disposition", attachment);

            page.Response.Charset = "";

     

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

     

            page.Response.Charset = "windows-1255";

            page.Response.ContentEncoding = Encoding.GetEncoding("windows-1255");

     

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);  

            HtmlForm frm = new HtmlForm();

            control.Parent.Controls.Add(frm);

            frm.Attributes["runat"] = "server";

            frm.Controls.Add(control);

           control.RenderControl(htw);

            page.Response.Write("<html><head><meta http-equiv='content-type' content='text/html; charset=windows-1255'></head><body>" + sw.ToString() + "</body></html>");

            page.Response.End();

    It bothers me that people still try to export to Excel this way.  You all know the extra overhead is using a GridView.  Pull the same dataset together and use something like NPOI to create the Excel workbook.  Moving from .xls to .xlsx would simply be a matter of changing HSSFWorkbook to XSSFWorkbook and changing the file extension of the downloaded file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 10, 2017 6:36 PM
  • User-359936451 posted

    Just FYI, sometimes the Gridview is needed to display the content on a web page prior to exporting.

    Wednesday, July 12, 2017 2:22 PM
  • User2048212178 posted

    I believe as the error statement correctly suggests the error is being caused due to invalid extension. Don’t worry there are several workarounds to this, I am sharing them below:

     

    FIX:

     

    Method 1 – You can Change the Default File Format to Save Excel Workbooks

    • Click File > Options.
    • Click on the Export option.
    • Tap on Change File Type option.
    • Change the file extension, and then click Save As.

    Method 2 – Recover the Unsaved Workbook

    • Navigate to File and select the Info option.
    • Below Manage Versions,  click on ‘Recover Unsaved Workbooks’ option.
    • If MS-Excel has unsaved files then, it will list them. You can therefore open and save it.

    Method 3 – Use the 'Open and Repair' Feature of MS Excel

    • Open MS-Excel application.
    • Navigate to File and Select Open option.
    • Select the corrupt file and choose Open and Repair option.

    Method 4 -  Use an Excel Repair Software   While going through such error, I stumbled onto this - https://social.technet.microsoft.com/Forums/office/en-US/63020ccc-51d7-46d9-b956-121c0e6efcc8/excel-file-error-the-file-format-and-extension-dont-match?forum=Office2016ITPro

    Tuesday, April 20, 2021 6:04 AM