yakatz
yakatz

Reputation: 2282

Detect Microsoft.Office.Interop.Excel Availability

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

Answers (5)

AdvApp
AdvApp

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

Mark W
Mark W

Reputation: 3909

this might work in the properties of your project:

enter image description here

http://s11.photobucket.com/albums/a199/markawil/?action=view&current=Untitled.png

Upvotes: -2

codechurn
codechurn

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

Darin Dimitrov
Darin Dimitrov

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

Related Questions