Is there a “proper” way to read CSV files [duplica

2019-01-10 19:52发布

Possible Duplicate:
CSV File Imports in .Net

In .net, is there a standard library that should be used to read in csv files? All the samples on the web roll their own csv reader / parser, or use OleDb.

It's not a problem using any of these solutions, I was just wondering if there is a generally accepted library (not that I can find), or any other "proper" way to do it?

标签: c# .net csv oledb
7条回答
成全新的幸福
2楼-- · 2019-01-10 20:00

One of the reasons that many people write their own is that CSV isn't quite so simple. For example:

  1. Does the first row contain field names, or not?
  2. Do you support dates? If, so, are they quoted, surrounded by # marks, in a certain day-month-year order?
  3. Does it support linefeeds that occur inside quoted text values? Or does that split the record?
  4. How do you escape a quote inside of a quoted string? Do you double the quote, or use a backslash or other escape character?
  5. What character encoding(s) are supported?
  6. How does it handle escaped control characters? &#XX; or \uXXXX or some other method?

These are some of the reasons people write their own parsers, because they're stuck reading files created with all these different settings. Or they write their own serializers, because the target system has a bunch of these idiosyncrasies.

If you don't care about these issues, just use the most convenient library. But understand they are there.

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-10 20:03

After some more investigation, there is also this: http://www.filehelpers.com/

It seems a full framework around reading files, and not just csv files.

(note: just read stuff on the website, have not used it yet)

查看更多
淡お忘
4楼-- · 2019-01-10 20:09

Try CsvHelper (a library I maintain). It's also available via NuGet.

CsvHelper allows you to read your CSV file directly into your custom class.

var streamReader = // Create a reader to your CSV file.
var csvReader = new CsvReader( streamReader );
List<MyCustomType> myData = csvReader.GetRecords<MyCustomType>();

CsvReader will automatically figure out how to match the property names based on the header row (this is configurable). It uses compiled expression trees instead of reflection, so it's very fast.

It is also very extensible and configurable.

查看更多
▲ chillily
5楼-- · 2019-01-10 20:10

I'm pretty sure you can read a CSV file into a DataTable with one line of code. Once it's in a DataTable, you can sort, filter, iterate etc.

This question has some examples for reading CSVs into DataTables.

查看更多
一夜七次
6楼-- · 2019-01-10 20:11

CsvReader is a pretty good one... it isn't Microsoft, but it works very well, and is a lot faster than some of the alternatives (legacy OleDb etc).

查看更多
Animai°情兽
7楼-- · 2019-01-10 20:15

The VB namespace has a great TextFieldParser class. I know, c# people don't like to reference a library from that 'basic' language, but it is quite good.

It is located at Microsoft.VisualBasic.FileIO.TextFieldParser

I used to mess with OLEDB, creating column definition files etc - but find the TextFieldParser a very simple and handy tool for parsing any delimited files.

查看更多
登录 后发表回答