Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Article

CSV to Table to CSV

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
13 Sep 2008CPOL3 min read 74.4K   3.6K   50   10
Utility class to import and export CSV files.

FileDetails.jpg

Introduction

This is a utility I use (converted from VB) to move data to and from CSV files and data tables. Hopefully, this will answer all those questions about moving CSV data to and from data tables.

Background

A lot of my work involves moving large amounts of data from legacy and core data systems into analysis and reporting systems. This is just one of the utilities I have developed over the years to meet those requirements.

This version does not support text delimiters (commas embedded in the data will break the split method).

I have kept clsFileHandler as a separate project as we include the file handler DLL in our projects. I assume that it will be included in a utilities project.

The sample CSV file is from the AdventureWorks contact table.

Using the code

  • GetDetails
  • This instantiates clsFileHandler using the file path from txtFileName. The constructor creates a new IO.FileInfo object and exposes it in the FileInf property. You can also pass in a populated FileInfo object. I also added the following properties as they are not in the FileInfo object for some reason.

    • UNCPath

      If the drive in the file name is a network drive, the UNC path is exposed; if it is a local drive the UNC path is blank.

    • NameOnly

      This exposes the file name without the extension. As I map file names to the data table when using BCP or BulkCopy and the table does not have an extension, this is a useful property.

  • CSVToTable
    • TitleRowNo: This indicates if there is to be a title row used for the table column headers. If there is no title, then leave the setting at -1.
    • FirstDataRow: This setting is to cater for when there is blank line(s) in the beginning of the file. It has the flexibility to support blank lines between the title row and the data.
    • FieldDelimiter: defaults to a comma.
    • MaxRows: defaults to 0 which will read all rows; setting this to a positive number limits the number of rows read. This is useful when test reading a large file.
  • GetData
  • This calls the method CSVtoTable. The problem here is to remember to set the data row and header row properties, becuase if these are incorrectly left to the defaults, they can really screw up your data.

C#
{
    oFH.Delimiter = txtDelimiter.Text;
    oFH.DataRow1 = Convert.ToInt32(numDataRow.Value);
    oFH.HeaderRow = Convert.ToInt32(numTitleRow.Value);
    oFH.MaxRows = (int)numMax.Value;
    dtData = oFH.CSVToTable();
    DataGridView1.DataSource = dtData;
}

Results where the title and first data row are set incorrectly:

TitleLoad1.jpg

After the correct settings are applied:

TitleLoad2.jpg

In previous versions, the settings have been passed in as parameters, and I'm not convinced that it is not a better solution.

  • CSVtoTable
  • This method does the following

    • Checks the settings and parameters.
    • Reads either the title row or the data row if there is no title.
    • Cleanses the text line of single quotes and slashes.
    • Uses the first row to create a table and name the columns based on the title or "ColName01" etc.; duplicate or blank named columns are dealt with in the same manner.
    • Reads each row and loads it into the data table.

    The method returns a populated data table.

  • TableToCSV
  • This method simply uses a stream writer to output the data to the file name specified. The usee can set the destination, the delimiter to use, and whether to include titles.

Points of interest

This utility needs to be extended to deal with text delimiters by reading each character in each line to determine the content. Another enhancement will be to export a list view to a CSV file.

History

First draft.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired None
Australia Australia
Started my programming life writing Excel 1.0 macros, God what a long time ago.

Now I'm a dotnet developer, I get to influence direction, play with new toys, build stuff, life is wonderful.

Greatest buzz you can get, walk past a row of desks and see your application running on all of them (and getting paid).

Greatest irritant, pouring 12 months of knowledge and experience into an empty head only to have it leave.

And now I'm retired, no deadlines, no meetings, no managers (except the ONE) and no users!

Comments and Discussions

 
Generalvery useful Pin
Southmountain31-Oct-15 9:42
Southmountain31-Oct-15 9:42 
GeneralDo you have something like this in VC++? Pin
novice514-May-11 20:52
novice514-May-11 20:52 
GeneralMy vote of 1 Pin
AndrewSmith2-Jan-09 14:39
AndrewSmith2-Jan-09 14:39 
GeneralNice! [modified] Pin
Ozitraveller15-Sep-08 14:09
Ozitraveller15-Sep-08 14:09 
GeneralRe: Nice! Pin
Mycroft Holmes15-Sep-08 14:22
professionalMycroft Holmes15-Sep-08 14:22 
GeneralRe: Nice! Pin
Ozitraveller15-Sep-08 15:45
Ozitraveller15-Sep-08 15:45 
GeneralRe: Nice! Pin
Mycroft Holmes15-Sep-08 15:57
professionalMycroft Holmes15-Sep-08 15:57 
GeneralRe: Nice! Pin
TobiasP16-Sep-08 0:31
TobiasP16-Sep-08 0:31 
GeneralRe: Nice! Pin
Mycroft Holmes16-Sep-08 0:54
professionalMycroft Holmes16-Sep-08 0:54 
Tobias
I agree that this does not meet all requirements, and it would certainly not support your numeric format, I have never had the dubious pleasure to have had to support this format. As for a specification I would be happy if I could get them to serve up a title line without blanks.

My old vb code actually has the option to read each character using the quotes to determine if it is inside a text string. Commas as decimals are nearly as bad as the US date format Dead | X| .


Never underestimate the power of human stupidity
RAH

GeneralRe: Nice! Pin
TobiasP16-Sep-08 1:27
TobiasP16-Sep-08 1:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.