Reading Excel files from C#

2018-12-31 01:22发布

Is there a free or open source library to read Excel files (.xls) directly from a C# program?

It does not need to be too fancy, just to select a worksheet and read the data as strings. So far, I've been using Export to Unicode text function of Excel, and parsing the resulting (tab-delimited) file, but I'd like to eliminate the manual step.

30条回答
泛滥B
2楼-- · 2018-12-31 01:50

You can try using this open source solution that makes dealing with Excel a lot more cleaner.

http://excelwrapperdotnet.codeplex.com/

查看更多
几人难应
3楼-- · 2018-12-31 01:50

you could write an excel spreadsheet that loads a given excel spreadsheet and saves it as csv (rather than doing it manually).

then you could automate that from c#.

and once its in csv, the c# program can grok that.

(also, if someone asks you to program in excel, it's best to pretend you don't know how)

(edit: ah yes, rob and ryan are both right)

查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 01:53

If it is just simple data contained in the Excel file you can read the data via ADO.NET. See the connection strings listed here:

http://www.connectionstrings.com/?carrier=excel2007 or http://www.connectionstrings.com/?carrier=excel

-Ryan

Update: then you can just read the worksheet via something like select * from [Sheet1$]

查看更多
墨雨无痕
5楼-- · 2018-12-31 01:54

Just did a quick demo project that required managing some excel files. The .NET component from GemBox software was adequate for my needs. It has a free version with a few limitations.

http://www.gemboxsoftware.com/GBSpreadsheet.htm

查看更多
君临天下
6楼-- · 2018-12-31 01:55

Excel Package is an open-source (GPL) component for reading/writing Excel 2007 files. I used it on a small project, and the API is straightforward. Works with XLSX only (Excel 200&), not with XLS.

The source code also seems well-organized and easy to get around (if you need to expand functionality or fix minor issues as I did).

At first, I tried the ADO.Net (Excel connection string) approach, but it was fraught with nasty hacks -- for instance if second row contains a number, it will return ints for all fields in the column below and quietly drop any data that doesn't fit.

查看更多
荒废的爱情
7楼-- · 2018-12-31 01:55

I just used ExcelLibrary to load an .xls spreadsheet into a DataSet. Worked great for me.

查看更多
登录 后发表回答