c# - Excel Interop 另存为在 excel 2007 中提供兼容性检查器

标签 c# excel excel-interop

这是我的 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/

相关文章:

c# - 无法在 C# 中打开 Excel 文件

c# - 未知数量类型参数的类设计

excel - 如何强制Excel在列中自动填充上一年而不是当前年份?

Excel 公式转 Google 文档格式

c# - 在 Excel 范围内指定列

c# - get_Offset 和 Offset 之间的区别?

c# - Lambda 函数处理程序 C# 中的取消标记

c# - 如何覆盖 PropertyGrid 中属性显示的文本

c# - AutoFixture:PropertyData 和异构参数

excel - 查找两个列表之间的匹配值