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条回答
余生请多指教
2楼-- · 2018-12-31 01:46

Late to the party, but I'm a fan of LinqToExcel

查看更多
听够珍惜
3楼-- · 2018-12-31 01:48

Lately, partly to get better at LINQ.... I've been using Excel's automation API to save the file as XML Spreadsheet and then get process that file using LINQ to XML.

查看更多
何处买醉
4楼-- · 2018-12-31 01:49

Koogra is an open-source component written in C# that reads and writes Excel files.

查看更多
步步皆殇っ
5楼-- · 2018-12-31 01:49

Not free, but with the latest Office there's a very nice automation .Net API. (there has been an API for a long while but was nasty COM) You can do everything you want / need in code all while the Office app remains a hidden background process.

查看更多
梦该遗忘
6楼-- · 2018-12-31 01:49

The solution that we used, needed to:

  • Allow Reading/Writing of Excel produced files
  • Be Fast in performance (not like using COMs)
  • Be MS Office Independent (needed to be usable without clients having MS Office installed)
  • Be Free or Open Source (but actively developed)

There are several choices, but we found NPoi (.NET port of Java's long existing Poi open source project) to be the best: http://npoi.codeplex.com/

It also allows working with .doc and .ppt file formats

查看更多
冷夜・残月
7楼-- · 2018-12-31 01:50

I did a lot of reading from Excel files in C# a while ago, and we used two approaches:

  • The COM API, where you access Excel's objects directly and manipulate them through methods and properties
  • The ODBC driver that allows to use Excel like a database.

The latter approach was much faster: reading a big table with 20 columns and 200 lines would take 30 seconds via COM, and half a second via ODBC. So I would recommend the database approach if all you need is the data.

Cheers,

Carl

查看更多
登录 后发表回答