tags:

views:

135

answers:

5

I need to read from a CSV/Tab delimited file and write to such a file as well from .net.

The difficulty is that I don't know the structure of each file and need to write the cvs/tab file to a datatable, which the FileHelpers library doesn't seem to support.

I've already written it for Excel using OLEDB, but can't really see a way to write a tab file for this, so will go back to a library.

Can anyone help with suggestions?

+1  A: 

Here are a couple CSV reader implementations:

http://www.codeproject.com/KB/database/CsvReader.aspx

http://www.heikniemi.fi/jhlib/ (just one part of the library; includes a CSV writer too)

I doubt there is a standard way to convert CSV to DataTable or database 'automatically', you'll have to write code to do that. How to do that is a separate question.

Qwertie
Why not just use the one that comes with the .NET framework?
Jonathan Allen
A: 

I used this CsvReader, it is really great and well configurable. It behaves well with all kinds of escaping for strings and separators. The escaping in other quick and dirty implementations were poor, but this lib is really great at reading. With a few additional codelines you can also add a cache if you need to.

Writing is not supported but it rather trivial to implement yourself. Or inspire yourself from this code.

jdehaan
Sorry.. it feels a little distant to me, but I guess my main consideration with writing csv files is delimiting the strings (it's been a while since I had to do csv files).
Tim Almond
I added a link for CSV writing, you can replace some constant strings with your preferred delimiter, and it should be ok. Reading is far more complicated than writing...
jdehaan
But writing CSV files which contain arbitrary strings is non-trivial. Remember escape sequences!
Ben Voigt
Why would you recommend this over the one that comes with .NET?
Jonathan Allen
Simply because I didn't know it :-). (However your TextFieldParser seems to be in the Microsoft.VisualBasic assembly which is not part of the standard .NET. Not sure what dependencies it has, but maybe he wants something for MONO too?)
jdehaan
A: 

You'll create your datatable in code, and (presuming a header row) can create columns based on your first line in the file. After that, it will simply be a matter of reading the file and creating new rows based on the data therein.

You could use something like this:

DataTable Tbl = new DataTable();
using(StreamReader sr = new StreamReader(path))
{
  int count = 0;
  string headerRow = sr.Read();
  string[] headers = headerRow.split("\t") //Or ","
  foreach(string h in headers)
  {
    DataColumn dc = new DataColumn(h);
    Tbl.Columns.Add(dc);
    count++;
  }
  while(sr.Peek())
  {
    string data = sr.Read();
    string[] cells = data.Split("\t") 
    DataRow row = new DataRow();
    foreach(string c in cells)
    {
      row.Columns.Add(c);
    }
    Tbl.Rows.Add(row);
  }
}

The above code has not been compiled, so it may have some errors, but it should get you on the right track.

AllenG
Your split code is wrong, as you may have the demliniator in a quoted string.
Jonathan Allen
@Jonathan Allen: Yes, indeed you may. It wasn't designed to be the full solution... just a pointer to a possible right track.
AllenG
A: 

I hope this following link will help you, please try this

This Contain Sample Code for how to read the Data from the Tab of Excel

http://www.codeproject.com/KB/office/excel_using_oledb.aspx

Ramakrishnan
+1  A: 

.NET comes with a CSV/tab delminited file parser called the TextFieldParser class.

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx

It supports the full RFC for CSV files and really good error reporting.

Jonathan Allen