CSV File Imports in .Net [closed]

2019-01-02 20:22发布

I realize this is a newbie question, but I'm looking for a simple solution - it seems like there should be one.

What's the best way to import a CSV file into a strongly-typed data structure? Again simple = better.

12条回答
浮光初槿花落
2楼-- · 2019-01-02 20:33

There are two articles on CodeProject that provide code for a solution, one that uses StreamReader and one that imports CSV data using the Microsoft Text Driver.

查看更多
无与为乐者.
3楼-- · 2019-01-02 20:34

A good simple way to do it is to open the file, and read each line into an array, linked list, data-structure-of-your-choice. Be careful about handling the first line though.

This may be over your head, but there seems to be a direct way to access them as well using a connection string.

Why not try using Python instead of C# or VB? It has a nice CSV module to import that does all the heavy lifting for you.

查看更多
ら面具成の殇う
4楼-- · 2019-01-02 20:41

If you're expecting fairly complex scenarios for CSV parsing, don't even think up of rolling our own parser. There are a lot of excellent tools out there, like FileHelpers, or even ones from CodeProject.

The point is this is a fairly common problem and you could bet that a lot of software developers have already thought about and solved this problem.

查看更多
不再属于我。
5楼-- · 2019-01-02 20:42

Microsoft's TextFieldParser is stable and follows RFC 4180 for CSV files. Don't be put off by the Microsoft.VisualBasic namespace; it's a standard component in the .NET Framework, just add a reference to the global Microsoft.VisualBasic assembly.

If you're compiling for Windows (as opposed to Mono) and don't anticipate having to parse "broken" (non-RFC-compliant) CSV files, then this would be the obvious choice, as it's free, unrestricted, stable, and actively supported, most of which cannot be said for FileHelpers.

See also: How to: Read From Comma-Delimited Text Files in Visual Basic for a VB code example.

查看更多
无色无味的生活
6楼-- · 2019-01-02 20:45

Use an OleDB connection.

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\InputDirectory\\;Extended Properties='text;HDR=Yes;FMT=Delimited'";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
DataTable dt = new DataTable();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM file.csv", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objAdapter1.Fill(dt);
objConn.Close();
查看更多
查无此人
7楼-- · 2019-01-02 20:45

I agree with @NotMyself. FileHelpers is well tested and handles all kinds of edge cases that you'll eventually have to deal with if you do it yourself. Take a look at what FileHelpers does and only write your own if you're absolutely sure that either (1) you will never need to handle the edge cases FileHelpers does, or (2) you love writing this kind of stuff and are going to be overjoyed when you have to parse stuff like this:

1,"Bill","Smith","Supervisor", "No Comment"

2 , 'Drake,' , 'O'Malley',"Janitor,

Oops, I'm not quoted and I'm on a new line!

查看更多
登录 后发表回答