代码之家  ›  专栏  ›  技术社区  ›  Water Cooler v2

有没有办法判断Microsoft Office文件是开放XML格式的?

  •  1
  • Water Cooler v2  · 技术社区  · 8 年前

    作为最终用户,我通常将文件扩展名更改为 .拉链 然后尝试提取存档。如果它确实提取,那么我知道它是一种开放的XML格式。但我甚至不确定这是否是一种简单的方法,它不适用的文件是否不是开放的Xml文件。

    CustomXmlParts 属性,我可以猜测这是我正在查看的一个开放XML格式的工作簿。

    PS C:\Users\Sathyaish> $excel = New-Object -ComObject Excel.Application
    PS C:\Users\Sathyaish> $workbook = $excel.Workbooks.Open("C:\Sathyaish\temp\Foo.xlsx")
    PS C:\Users\Sathyaish> $workbook | select C*, F*
    
    
    Creator                  : 1480803660
    ChangeHistoryDuration    : 0
    Charts                   : System.__ComObject
    CodeName                 :
    CommandBars              :
    Comments                 :
    ConflictResolution       : 1
    Container                :
    CreateBackup             : False
    CustomDocumentProperties : System.__ComObject
    CustomViews              : System.__ComObject
    CalculationVersion       : 171027
    ContentTypeProperties    :
    Connections              : System.__ComObject
    CheckCompatibility       : False
    CustomXMLParts           : System.__ComObject
    ConnectionsDisabled      : False
    CaseSensitive            : False
    ChartDataPointTrack      : True
    FileFormat               : 51
    FullName                 : C:\Sathyaish\temp\Foo.xlsx
    FullNameURLEncoded       : C:\Sathyaish\temp\Foo.xlsx
    Final                    : False
    ForceFullCalculation     : False
    

    有没有VSTO/VBA方法可以告诉你同样的事情?也许通过观察 FileFormat

    或者,必须可以选择使用Open XML SDK加载文档,如果失败,那么您就知道它不是一种合适的Open XML文件格式。但这并不排除其他可能性,例如该文件根本不是Microsoft Office文件。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Chris    8 年前

    Excel的 FileFormat 属于类型 Microsoft.Office.Interop.Excel.XlFileFormat

       //
        // Summary:
        //     Specifies a type of text format
        xlCurrentPlatformText = -4158,
        //
        // Summary:
        //     Excel workbook format.
        xlWorkbookNormal = -4143,
        //
        // Summary:
        //     Symbolic link format.
        xlSYLK = 2,
        //
        // Summary:
        //     Lotus 1-2-3 format.
        xlWKS = 4,
        //
        // Summary:
        //     Lotus 1-2-3 format.
        xlWK1 = 5,
        //
        // Summary:
        //     Comma separated value.
        xlCSV = 6,
        //
        // Summary:
        //     Dbase 2 format.
        xlDBF2 = 7,
        //
        // Summary:
        //     Dbase 3 format.
        xlDBF3 = 8,
        //
        // Summary:
        //     Data Interchange format.
        xlDIF = 9,
        //
        // Summary:
        //     Dbase 4 format.
        xlDBF4 = 11,
        //
        // Summary:
        //     Deprecated format.
        xlWJ2WD1 = 14,
        //
        // Summary:
        //     Lotus 1-2-3 format.
        xlWK3 = 15,
        //
        // Summary:
        //     Excel version 2.0.
        xlExcel2 = 16,
        //
        // Summary:
        //     Excel template format.
        xlTemplate = 17,
        //
        // Summary:
        //     Template 8
        xlTemplate8 = 17,
        //
        // Summary:
        //     Microsoft Office Excel Add-In.
        xlAddIn = 18,
        //
        // Summary:
        //     Microsoft Excel 97-2003 Add-In
        xlAddIn8 = 18,
        //
        // Summary:
        //     Specifies a type of text format.
        xlTextMac = 19,
        //
        // Summary:
        //     Specifies a type of text format.
        xlTextWindows = 20,
        //
        // Summary:
        //     Specifies a type of text format.
        xlTextMSDOS = 21,
        //
        // Summary:
        //     Comma separated value.
        xlCSVMac = 22,
        //
        // Summary:
        //     Comma separated value.
        xlCSVWindows = 23,
        //
        // Summary:
        //     Comma separated value.
        xlCSVMSDOS = 24,
        //
        // Summary:
        //     Deprecated format.
        xlIntlMacro = 25,
        //
        // Summary:
        //     Microsoft Office Excel Add-In international format.
        xlIntlAddIn = 26,
        //
        // Summary:
        //     Excel version 2.0 far east.
        xlExcel2FarEast = 27,
        //
        // Summary:
        //     Microsoft Works 2.0 format
        xlWorks2FarEast = 28,
        //
        // Summary:
        //     Excel version 3.0.
        xlExcel3 = 29,
        //
        // Summary:
        //     Lotus 1-2-3 format.
        xlWK1FMT = 30,
        //
        // Summary:
        //     Lotus 1-2-3 format.
        xlWK1ALL = 31,
        //
        // Summary:
        //     Lotus 1-2-3 format.
        xlWK3FM3 = 32,
        //
        // Summary:
        //     Excel version 4.0.
        xlExcel4 = 33,
        //
        // Summary:
        //     Quattro Pro format.
        xlWQ1 = 34,
        //
        // Summary:
        //     Excel version 4.0. Workbook format.
        xlExcel4Workbook = 35,
        //
        // Summary:
        //     Specifies a type of text format.
        xlTextPrinter = 36,
        //
        // Summary:
        //     Lotus 1-2-3 format.
        xlWK4 = 38,
        //
        // Summary:
        //     Excel version 5.0.
        xlExcel5 = 39,
        //
        // Summary:
        //     Excel 95.
        xlExcel7 = 39,
        //
        // Summary:
        //     Deprecated format.
        xlWJ3 = 40,
        //
        // Summary:
        //     Deprecated format.
        xlWJ3FJ3 = 41,
        //
        // Summary:
        //     Specifies a type of text format.
        xlUnicodeText = 42,
        //
        // Summary:
        //     Excel version 95 and 97.
        xlExcel9795 = 43,
        //
        // Summary:
        //     Web page format.
        xlHtml = 44,
        //
        // Summary:
        //     MHT format.
        xlWebArchive = 45,
        //
        // Summary:
        //     Excel Spreadsheet format.
        xlXMLSpreadsheet = 46,
        //
        // Summary:
        //     Excel12
        xlExcel12 = 50,
        //
        // Summary:
        //     Open XML Workbook
        xlOpenXMLWorkbook = 51,
        //
        // Summary:
        //     Workbook default
        xlWorkbookDefault = 51,
        //
        // Summary:
        //     Open XML Workbook Macro Enabled
        xlOpenXMLWorkbookMacroEnabled = 52,
        //
        // Summary:
        //     Open XML Template Macro Enabled
        xlOpenXMLTemplateMacroEnabled = 53,
        //
        // Summary:
        //     Open XML Template
        xlOpenXMLTemplate = 54,
        //
        // Summary:
        //     Open XML Add-In
        xlOpenXMLAddIn = 55,
        //
        // Summary:
        //     Excel8
        xlExcel8 = 56,
        //
        // Summary:
        //     OpenDocument Spreadsheet
        xlOpenDocumentSpreadsheet = 60
    

    您应该能够在PowerShell中将该整数值强制转换为XlFileFormat对象,但在我脑海中,我不知道具体如何转换。这应该是一些变化(例如C#):

    Enum.Parse(typeof(Microsoft.Office.Interop.Excel.XlFileFormat), fileFormat)