tags:

views:

596

answers:

3

Is it possible to extract information from an excel file (.xls) into c# on a computer without excel installed?

I've got the following code:

     OleDbConnection objConn = new 
          OleDbConnection(CONNECTION_STRING.Replace("<FILENAME>", fileName));

            try
            {
                objConnection.Open(); 
            }
            catch (Exception)
            {}

It throws an IndexOutOfRangeException ("Cannot find table 0") when I try to open the OleDbConnection when run on a computer that does not have excel installed. The same code run on a computer with excel works just fine. I therefore very strongly suspect the lack of excel to be the culprit.

Is this the problem? If this is, how can I extract data from the file?

+1  A: 

It's possible. If the format is .XLS use Microsoft Jet OLEDB 4.0 (should work without problems as long as it's run on a 32 bit system - there are some hick-ups with 64 bit systems) - which I suspect that you've already messed around with a bit. You're probably on the right path.

Check this link out, it should explain at least some of what you need to know:

Reading and Writing Excel Spreadsheets Using ADO.NET C# DbProviderFactory

Marcus L
+3  A: 

There are two main methods without Excel installed

Use the Jet database drivers to open the file. This will give you access to the cell values in the rows, treating each sheet as a table. You will not have access to any of the meta-information (formatting, comments, etc).

Use SpreadsheetGear.NET, which allows you to open, create, manipulate and save Excel binary files without Excel or any other dependencies. SpreadSheetGear can be downloaded for evaluation, but costs money to licence.

David
+3  A: 

Check out the open-source NExcel. Last updated about 2 years ago, so it doesn't have support for the newer Excel 2007 .xlsx format, but will read Excel 07-Excel 2003.

Jesse C. Slicer