6

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?

yakatz
  • 2,142
  • 1
  • 18
  • 47

5 Answers5

7

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Checking the registry seems weird to me, but if that is the way it is usually done, that I suppose I will. – yakatz Feb 08 '12 at 04:43
  • 2
    How is this the answer? Checking if office is installed will not necessarily mean the interop components are also installed. The question is wheter or not the interop assemblies can be detected and not how to add them to the installer prereqs. Assemblies can be detected, using reflection. – Kris Feb 08 '12 at 13:17
1

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.

codechurn
  • 3,870
  • 4
  • 45
  • 65
  • 1
    This does not solve the original question. Your proposal stil requires an external dependency for which ultimately you would still need to check... – Kris Feb 07 '12 at 22:13
  • What external dependancy? He would need to reference the Document.OpenXML assembly from his project and could deploy it with his application. The end user would not need to have Excel installed. Likewise, the Microsoft Jet OLEDB 4.0 provider is present with newer versions of the Windows operating system and could be leveraged by his application to read older format spreadsheets not supported by the OpenXML assembly and CSV files if necessary. – codechurn Feb 08 '12 at 00:27
  • @ Art: The question was about detecting an assembly. Not about including it as a prerequisite... Your solution is just the same as his initial problem. The end user would indeed not need to have Excel installed but OpenOffice or something els instead... – Kris Feb 08 '12 at 13:19
  • Incorrect. He would not need to have OpenOffice installed. The OpenXML SDK allows you to build an applictaion which can read/write Microsoft Office file formats. The question was about importing data from serveral formats (of which Excel and CSV were included). The poster ASSUMED that Excel Interop was his best option, thus prompting the follow on about the availability of the Excel.Interop on a machine. My response was that Excel.Interop is NOT the only option, and by using an alternative such as OpenXML, the dependancy for checking for the presence of assemblies becomes moot. – codechurn Feb 08 '12 at 16:16
0

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;
}
AdvApp
  • 1,094
  • 1
  • 14
  • 27
0

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

Community
  • 1
  • 1
Kris
  • 2,100
  • 5
  • 31
  • 50
-2

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

Mark W
  • 3,879
  • 2
  • 37
  • 53