Reputation: 2282
I am working on an application that can import data from several different formats. I would like to include CSV and other flat-file types (easy) as well as XLS and XLSX.
It looks like my best option is to use Interop.Excel which I understand is only available if Excel is installed.
Is there a way to check whether Interop.Excel is available and tell the user that Excel must be installed to import from XLS/XLSX?
Upvotes: 6
Views: 7483
Reputation: 1170
Just attempt to create an instance of Excel. If it produces an exception, Excel is not installed and the interop will not work. If no exception, then you can use the interop.
using Excel = Microsoft.Office.Interop.Excel;
...
private bool CheckForExcel()
{
bool exists = false;
try
{
var excel = new Excel.Application();
exists = true;
}
catch (Exception)
{
;
}
return exists;
}
Upvotes: 0
Reputation: 3909
this might work in the properties of your project:
http://s11.photobucket.com/albums/a199/markawil/?action=view¤t=Untitled.png
Upvotes: -2
Reputation: 3990
If you want to support native Microsoft Excel files and do not want to take a dependancy on Microsoft Excel, then look into OpenXML. This will only work with the newer Excel XML formated files. If your needs are simple and you just need to read data out of the legacy formated Excel file or CSV file you may be able to use the Ace provider and/or Microsoft Jet OLE DB 4.0 providers
OpenXML allows you to read/write to new XML formated Excel files. There are several threads on StackOverflow with more info on using OpenXML that are worth checking out.
Upvotes: 1
Reputation: 2123
You could try to use the concepts detailed here:
How to detect .NET 2.0 vs. .NET 4.0 assemblies, and x86 vs. x64 vs. AnyCPU
But also:
How to detect if another assembly is available to my application or not
Upvotes: 0
Reputation: 1039498
Microsoft.Office.Interop.Excel
is an Office Primary Interop Assembly that you could ship with your application no matter whether the client has Office installed or not. It won't do any harm until you start using it. So before using it you could look at the registry to see if Office is installed.
Upvotes: 7