Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good CSV Writer for C#? [duplicate]

Tags:

c#

Possible Duplicate:
Writing a CSV file in .net

Are there any good CSV writers for C#?

Don't need a reader, just writer.

like image 376
mpen Avatar asked Jan 13 '11 21:01

mpen


People also ask

Can we read csv file in C?

Open CSV File using File Pointer in append mode which will place a pointer to the end of the file. Take Input from the user in temporary variables. Use fprintf() and separate variables according to their order and comma.

Is CSV helper free?

Many contributors have helped make CsvHelper the great library it is today. Completely free for commercial use.

How do I create a CSV file in C++?

If you wirte to a . csv file in C++ - you should use the syntax of : myfile <<" %s; %s; %d", string1, string2, double1 <<endl; This will write the three variables (string 1&2 and double1) into separate columns and leave an empty row below them.


2 Answers

There's not much to it, really... here's some code I've used for several years:

public static class Csv
{
    public static string Escape( string s )
    {
        if ( s.Contains( QUOTE ) )
            s = s.Replace( QUOTE, ESCAPED_QUOTE );

        if ( s.IndexOfAny( CHARACTERS_THAT_MUST_BE_QUOTED ) > -1 )
            s = QUOTE + s + QUOTE;

        return s;
    }

    public static string Unescape( string s )
    {
        if ( s.StartsWith( QUOTE ) && s.EndsWith( QUOTE ) )
        {
            s = s.Substring( 1, s.Length - 2 );

            if ( s.Contains( ESCAPED_QUOTE ) )
                s = s.Replace( ESCAPED_QUOTE, QUOTE );
        }

        return s;
    }


    private const string QUOTE = "\"";
    private const string ESCAPED_QUOTE = "\"\"";
    private static char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' };
}

You can use the Escape method to ensure that values are properly quoted. I use this class in conjunction with a simple reader, but you said you don't need that...

Update It's simple, but there is more to it than string.Join. However, you can still get away with something pretty simple, if you have an array of values (and are using C# 3+):

string.Join(",", values.Select(Csv.Escape));
like image 134
harpo Avatar answered Sep 27 '22 19:09

harpo


Writing is fairly trivial, but libraries can do a lot of nice things for you. Such as writing out custom objects.

Here is an example of CsvHelper (a library I maintain) writing objects.

// Custom object.
public class MyCustomObject
{
    public string StringProperty { get; set; }
    public int IntProperty { get; set; }
}

// Writing the CSV file.
var myCustomObjectList = new List<MyCustomObject>
{
    new MyCustomObject { StringProperty = "one", IntProperty = 1 },
    new MyCustomObject { StringProperty = "two", IntProperty = 2 }
};
var csv = new CsvHelper( File.OpenWrite( "some-file.csv" ) );
csv.Writer.WriteRecords( myCustomObjectList );

By default, conventions are used for writing the headers. This is all configurable.

// Output:
StringProperty,IntProperty
one,1
two,2
like image 44
Josh Close Avatar answered Sep 27 '22 21:09

Josh Close