Reputation:
I need to use Excel
if it is installed on the target machine, or produce a text
file if it is not.
I check for the presence of the excel application
registry key, but before I can do that, I get a
System.IO.FileNotFoundException: Could not load file or assembly Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c
or one of its dependencies.
How can I detect if an interop
assembly is available and prevent the run-time
trying to load it if it isn't. (I don't know what version of Excel
may exist on the target, so I don't think I can redistribute an interop
in case its the wrong one.)
Upvotes: 1
Views: 379
Reputation: 131237
It seems that you added a reference to a specific interop assembly in your code. Your application will not even load since it can't find all the assemblies it needs to run. If you absolutely have to use Excel through Interop, you will have to remove the reference and load the assembly dynamically with Assembly.Load.
You don't need Excel at all if all you want is to read/create an Excel file.
XLSX is just a bunch of zipped XML files. The OpenXML SDK by Microsoft allows you to manipulate the files withoud dealing with the XML directly and there are other libraries that sit on top of the SDK to make programming even easier.
Generating Excel 2010 Workbooks by using the Open XML SDK 2.0 offers a pretty detailed explanation of the XLSX format and how you can use the SDK to create simple and complex spreadsheets easily.
Upvotes: 2