88

is there a good and free implementation of CSV parser available under some liberal licence? Some counterpart of SuperCSV for Java, perhaps a port?

Bartosz Radaczyński
  • 18,396
  • 14
  • 54
  • 61

6 Answers6

71

FileHelpers Open Source Library.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Keltex
  • 26,220
  • 11
  • 79
  • 111
  • 24
    FileHelpers (for CSV at least) requires that you "define a class that maps to the record in the source (file)", "you must declare a Record Mapping Class" etc. and this is not so hot. I'd like to convert CSV into a DataTable, not knowing in advance how many columns to expect. – Konrad Morawski Jul 07 '11 at 13:00
53

There's a nice implementation on CodeProject:

To give more down to earth numbers, with a 45 MB CSV file containing 145 fields and 50,000 records, the reader was processing about 30 MB/sec. So all in all, it took 1.5 seconds! The machine specs were P4 3.0 GHz, 1024 MB.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • (taking back my +1): I just broke the lumenworks Fast CSV reader on a 53Mb file. Looks like the line caching failed after 43,000 rows and scrambled the buffer. Tried the `Microsoft.VisualBasic.FileIO.TextFieldParse` and it did the trick. – iCollect.it Ltd May 21 '12 at 12:55
12

You can load a CSV file to DataTable.

Sample code -

static DataTable CsvToDataTable(string strFileName)
{
    DataTable dataTable = new DataTable("DataTable Name");

    using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Directory.GetCurrentDirectory() + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""))
    {
        conn.Open();
        string strQuery = "SELECT * FROM [" + strFileName + "]";
        OleDbDataAdapter adapter = 
            new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
        adapter.Fill(dataTable);
    }
    return dataTable;
}

Make sure you compile your project to x86 processor. It doesn't work for x64.

RB.
  • 36,301
  • 12
  • 91
  • 131
Maxim
  • 7,268
  • 1
  • 32
  • 44
  • 1
    this worked pretty well for me since I wanted to stay with the built-in ODBC or OLEDB libraries. Btw, the following has additional code samples for both OLEDB and ODBC: http://www.csvreader.com/csv_benchmarks.php – Meringros Oct 03 '10 at 19:12
  • 5
    This is not working on 64 bit, alas. – DenNukem Nov 15 '12 at 22:07
  • Two links that maybe useful with regards to this answer: 1. [C# Tutorial - Using The Built In OLEDB CSV Parser](http://tech.pro/tutorial/803/csharp-tutorial-using-the-built-in-oledb-csv-parser) 2. [How To Retrieve Column Schema by Using the DataReader GetSchemaTable Method and Visual C# .NET](http://support.microsoft.com/kb/310107) – shawad Jul 25 '13 at 16:36
7

try filehelpers Work amazingly well. I am using it to parse a 100 MB file every day.

Marcos Meli
  • 3,468
  • 24
  • 29
no_one
  • 1,852
  • 12
  • 11
4

Have you tried the FileHelpers library? It's free, open source and can be used to parse CSV files.

Marcos Meli
  • 3,468
  • 24
  • 29
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3

I've started using CSV Parser that is part of the CommonLibrary.NET.

It uses .NET 3.5, has an easy API, and convenient overloads/methods & lamda's for iterations.

I don't have any benchmarks for this one like above, but nice thing about this is that it's just one component of a library similar to Java Commons. So I also get a Command-line parser, Repository implementation among other things.

zhao
  • 31
  • 1