views:

83

answers:

6

I'm working on a VS 2008 C# program that needs to get data out of an excel spreadsheet. Problem is that the users run a mix of office 2007 and Office 2010. So I'm trying to get some pointers in the right direction on a way to programmatically get data out of the xls that doesn't care which version of office the user has installed.

Bonus points if it will compile in both environments (VS2008/Office2007 and VS2008/Office2010)

+6  A: 

You can use OleDB.

Note that their example is incorrect and needs to use an OleDbConnectionStringBuilder, like this:

OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();

if (isOpenXML)
    builder.Provider = "Microsoft.ACE.OLEDB.12.0";
else
    builder.Provider = "Microsoft.Jet.OLEDB.4.0";

builder.DataSource = fileName;
builder["Extended Properties"] = "Extended Properties=\"Excel 8.0;HDR=YES;\""

con = new OleDbConnection(builder.ToString());  
SLaks
Beware that Microsoft.Jet.OLEDB.4.0 is not available in 64bit http://stackoverflow.com/questions/861022/oledb-not-supported-in-64bit-mode, http://stackoverflow.com/questions/861022/oledb-not-supported-in-64bit-mode
Tomas Voracek
so changing the project to target x86 was a viable option on this project, which side-stepped the x64 problem.
Drogo
A: 

You don't have to use office automation at all. There are many packages for reading Excel files from .Net. Some are commercial and some are open source.

A random search at codeplex.com returned this one for instance: Excel Data Reader

Rune Grimstad
A: 

If you really want to use Excel itself to read the data then you should reference the lowest version of Excel that you expect your users to have installed. Excel 2010 is backwards compatible with Excel 2007 and should support applications written against the 2007 library.

Rune Grimstad
A: 

Not sure if this is a project in which you have a budget to work with....

At our company, we have used SpreadsheetGear and it has been awesome to work with. If you've got the funds for it, it is a great tool to have at your disposal when it comes to working with excel files. SpreadsheetGear can help with any kind of excel file and it does not require the user to have excel installed.

Anthony
No budget for extra tools, I'll have to keep that in mind for future projects though...
Drogo
A: 

Just export the data to a csv or txt file, and it will be version agnostic.

Lance Roberts
That would've been my default answer except for the width of the spreadsheet (several hundred columns) and the age of some of the users
Drogo
A: 

This one is free and easy to use.

http://npoi.codeplex.com/

liya