The CsvFileReader class comes after that. It also provides a way to change the characters used as delimiters and quotes. There are a few settings common to both the reader and writer classes and so I use this abstract base class to track the special characters within a CSV value that require the value to be enclosed in quotes. Note that this does not affect empty lines within a quoted value, or an empty line at the end of the input file. So the CsvFileReader class' constructor takes an argument of this type to specify how empty lines should be handled. After careful review, I realized there were a few valid ways to interpret an empty line within a CSV file. Starting at the top of the code is the EmptyLineBehavior enum. This also simplifies the code because neither class needs to worry about which mode the file is in or protect against the user switching modes. NET stream classes generally seem to be split into reading and writing, I decided to follow that same pattern with my CSV code and split it into CsvFileReader and CsvFileWriter. Propagate Dispose to StreamWriter public void Dispose()īecause the. Writer.Write(QuotedFormat, columns.Replace(OneQuote, TwoQuotes)) Write this column if (columns.IndexOfAny(SpecialChars) = -1) Write each column for ( int i = 0 i 0) / protected char SpecialChars = new char ", Quote) If a column contains any /// of these characters, the entire column is wrapped in double quotes.
/ /// These are special characters in CSV files. / /// Common base class for CSV reader and writer classes. / /// An empty line is interpreted as the end of the input file. / /// Empty lines are skipped over as though they did not exist. / /// Empty lines are interpreted as a line with a single empty column. / /// Empty lines are interpreted as a line with zero columns.
/ These values do not affect empty lines that occur within quoted fields /// or empty lines that appear at the end of the input file. Listing 1: CsvFileReader and CsvFileWriter Classes /// /// Determines how empty lines are interpreted when reading CSV files. Listing 1 shows my CsvFileReader and CsvFileWriter classes. So this seems like a perfect task for a handy little C# class. In addition, double quotes that exist within a double quoted value must appear as two double quote characters together to distinguish them from the double quote character at the end of the value. This is required to prevent those special characters from being interpreted as value delimiters, etc. Double quotes are used to wrap values that contain special characters such as commas, double quotes, new lines, etc. However, there is slightly more work involved. Each value is a field (or column in a spreadsheet), and each line is a record (or row in a spreadsheet). As the name suggestions, a CSV file is simply a plain text file that contains one or more values per line, separated by commas. CSV files can easily be read and written by many programs, including Microsoft Excel.įor the most part, reading and writing CSV files is trivial. Comma-Separated Values (CSV) FilesĪ much simpler way to have your application share data is by reading and writing Comma-Separated Values (CSV) files. I also added several options to control how the CSV reader class handles empty lines. I decided to make the code more robust and add a number of new features that include support for multi-line values and the ability to change the characters used for delimiters and quotes. Note that the code below is a complete rewrite of the code presented when this article was first published. Although there are interfaces available to work with, for example, Microsoft Excel data files, this approach is generally complex, involves a fair amount of overhead, and requires that support libraries accompany your application. A common requirement is to have applications share data with other programs.