这是我的 SaveAs
方法的代码:
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);
现在我的问题不是如何禁用兼容性检查器,这个问题已经被回答过很多次了。我的问题是为什么兼容性检查器会出现。 excel如何知道该文件有excel 2003的痕迹。D:\Build为空。
最佳答案
如果您想创建特定版本的 Excel 文件,您必须设置正确的 FileFormat
在 SaveAs
方法。
即使你设置.xls
路径中,如果不指定文件格式,将使用运行版本的Excel来保存文件,使用SaveAs
方法。这意味着,如果您使用 Excel 2007 或更高版本打开 Excel 1997-2003 文件,SaveAs
如果您不设置FileFormat
,方法将默认将文件保存为Excel 2007文件。参数。
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?
不要像您那样设置文件格式或 xlWorkbookDefault
是要在 2003 和 2007 上保存时使用的文件格式,具体取决于系统中安装的 Excel。
您需要检测当前的 Excel 版本,以便在文件名中设置正确的扩展名(.xls 或 .xlsx)。如果扩展名与使用的实际文件格式不对应,当您打开文件时,您将收到一条消息,指出扩展名与文件格式不对应。
或者,您提出的解决方案似乎没有在fileName中设置扩展名,而是让Excel在检测Excel版本后根据您指定的fileFormat用正确的扩展名完成fileName。您在这里使用的是:
- 56:
xlExcel8
Excel8 - 46:
xlXMLSpreadsheet
Excel 电子表格格式。
关于 FileFormat
的链接
另请注意,如果您在 Excel 2003 文件中使用某些 Excel 2007 功能,它不会阻止显示兼容性检查器。要隐藏该消息,您可以设置 Application.DisplayAlerts
属性至false
.
关于c# - Excel Interop 另存为在 excel 2007 中提供兼容性检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18416867/