Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading/writing CSV/tab delimited files in c#

Tags:

c#

csv

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?

like image 735
Tim Almond Avatar asked Jun 28 '10 17:06

Tim Almond


People also ask

How do I read a CSV file in delimiter?

Using the "From Text" feature in Excel Click the Data tab, then From Text. Select the CSV file that has the data clustered into one column. Select Delimited, then make sure the File Origin is Unicode UTF-8. Select Comma (this is Affinity's default list separator).

Can a CSV file be tab delimited?

A CSV (Comma Separated Values) or Tab-delimited Text (or Tab Separated Values) file is a text file in which one can identify rows and columns. Rows are represented by the lines in the file and the columns are created by separating the values on each line by a specific character, like a comma or a tab.


1 Answers

Simple example with CsvHelper

using (TextWriter writer = new StreamWriter(filePath)
{
    var csvWriter = new CsvWriter(writer);
    csvWriter.Configuration.Delimiter = "\t";
    csvWriter.Configuration.Encoding = Encoding.UTF8;
    csvWriter.WriteRecords(exportRecords); 
}
like image 155
Wieslaw Olborski Avatar answered Oct 15 '22 23:10

Wieslaw Olborski