简体   繁体   中英

Microsoft Excel Interop Version

I have a Windows Forms application that gets deployed to many user machines throughout our network. Some of our users have Excel 2010, some with 2013 and others with 2016.

In the past, I've instructed development staff to compile with Interop version 14, to ensure interoperability across versions. Unfortunately, a couple times, that request has not been honored, and we have had user issues.

Understanding that I'm not the only coding, how best to ensure that when the application is compiled, it is compiled with interop version 14.

Some options I have considered include:

  • Add something into the program to check the interop when the app runs, if its not version 14, throw an error. While an odd approach, it will ensure that the developers cannot test their code if its using the wrong interop version
  • A post build event. I'm not sure if this is right, or exactly how to do it.

Looking for any recommendations. Thanks.

I would not use interop at all. To many things can go wrong. I would use a library like NPOI or OpenXML sdk to create the spreadsheet.

https://github.com/tonyqus/npoi

http://vbnpoi.blogspot.com/

https://msdn.microsoft.com/en-us/library/office/bb448854.aspx

I have a method I'm going to put in a DLL:

''' <summary>
''' Checks the Version of Excel
''' It's Purpose Is To Ensure We Don't Deploy Wrong Interop Version
''' </summary>
''' <remarks></remarks>
Public Shared Sub CheckVersion(ByVal typeName As String, ByVal maximumVersion As Integer)

    Try

        Dim a2 As System.Reflection.Assembly = System.Reflection.Assembly.ReflectionOnlyLoad(typeName)
        Dim majorVersion As Integer = a2.GetName().Version.Major
        If majorVersion > maximumVersion Then
            Throw New ApplicationException("Invalid Version of " & typeName & " - Using Version " & majorVersion & " - Maximum Allowed Version " & maximumVersion)
        End If
    Catch iex As System.IO.FileNotFoundException
        Trace.WriteLine("VersionChecker - CheckVersion " & iex.Message & Environment.NewLine & iex.StackTrace)
        Throw New ApplicationException("File Not Found " & typeName & " - Note only works for interop types when Embed interop types is disabled and CopyLocal is true.", iex)
    Catch ex As Exception
        Trace.WriteLine("VersionChecker - CheckVersion " & ex.Message & Environment.NewLine & ex.StackTrace)
        Throw ex
    End Try

End Sub

Then put it into the program like this:

VersionChecker.CheckVersion("Microsoft.Office.Interop.Excel", 14)

It'll bomb if they use the wrong one on the load of the program, ensuring they always do it right.

I won't accept, perhaps something better comes along.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM