开发者

Office-agnostic way to access data in a .xls file?

开发者 https://www.devze.com 2023-01-17 23:09 出处:网络
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开发者_

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)


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());  


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.


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


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.


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


This one is free and easy to use.

http://npoi.codeplex.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消