How can I check the version of Excel files in C#?

前端 未结 3 1080
臣服心动
臣服心动 2021-01-25 00:44

I need to differentiate that a selected file is created with Excel 2010 or Excel 2013 version of a selected Excel file and Excel application on server must match in order to con

3条回答
  •  伪装坚强ぢ
    2021-01-25 01:15

    For .xls files you need to decode the "BIFF" value, from MSDN: How To Determine the Version of a Microsoft Excel Workbook:

    Microsoft Excel saves data using structured storage. In particular, it creates a data stream called "Workbook" (previously just "Book") where it saves the contents starting with a BOF (beginning of file) record.

    There's also some code in that article but I'm unsure how much I can lift from it without breaching copyright.

    That format is used up to Excel 2002 according to a table at the bottom of that article.

    As for .xlsx files, you can use any zip file library to open it, then open the docProps\app.xml file, and inside you'll find this:

    
    
        ....
        15.0300
    
    

    This will tell you the version of Excel that saved that particular file.

    Also note that Excel will determine whether to use new storage loading or old storage loading depending on the contents of the file and not only on the extension, as such you can store an old-style file with a .xlsx extension and a new-style file with a .xls extension and Excel will still open it, and so should you. Excel will give a warning though, how you handle the discrepancy is up to you.

提交回复
热议问题