Excel Interop Save as is giving compatibility chec

2019-02-15 13:43发布

问题:

Here is my code for SaveAs method:

m_FilePath="D:\Build\abc.xlsx"
m_objOpt=System.Reflection.Missing.Value;
m_objBook.SaveAs(m_FilePath, m_objOpt, m_objOpt,
    m_objOpt, m_objOpt, m_objOpt, XlSaveAsAccessMode.xlShared,
    m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

Now my question is not how to disable compatibility checker, that is already been answered many times. My question is why compatibility checker is coming at all. How does excel comes to know that the file is having traces of excel 2003. D:\Build is empty.

回答1:

If you want to create a specific version of Excel file, you have to set the correct FileFormat In the SaveAs method.

Even if you set .xls in the path, if the file format is not specified, the running version of Excel will be used to save the file using the SaveAs method. It means that if you open an Excel 1997-2003 file with Excel 2007 or higher, the SaveAs method will save by default the file as Excel 2007 file if you don't set the FileFormat parameter.

Optional Object. The file format to use when you save the file. For a list of valid choices, see the FileFormat property. For an existing file, the default format is the last file format specified; for a new file, the default is the format of the version of Excel being used.


What should be my FileFormat then if i want to save on both 2003 and 2007 depending on which excel is installed in the system?

Do not set the fileformat like you do or xlWorkbookDefault is the fileformat to use if you want to save on both 2003 and 2007 depending on which Excel is installed in the system.

What you need is to detect the current version of Excel in order to set the correct extension (.xls or .xlsx) in the file name. If the extension doesn't correspond to the real fileFormat which is used, you will have a message saying the extension doesn't correspond to the fileformat when you will open the file.

Or, the solution you propose seems to not set the extension in fileName, and let Excel complete the fileName with the correct extension based on the fileFormat that you specify after detecting the Excel version. Here you are using :

  • 56 : xlExcel8 Excel8
  • 46 : xlXMLSpreadsheet Excel Spreadsheet format.

Link about FileFormat


Also note that it won't prevent the compatibility checker to be displayed if you are using some Excel 2007 functionnality in an Excel 2003 file. To hide the message, you could set the Application.DisplayAlerts property to false.



回答2:

I did something like this :

if (11.0 < Convert.ToDouble(report.Version))
          {
              //For excel 2003
              workbook.SaveAs(filename, 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
           }
          else
          {
            //For excel 2007 and above
        workbook.SaveAs(filename, 51, misval, misval, misval, misval, XlSaveAsAccessMode.xlShared, misval, misval, misval, misval, misval);
            }