CSV export

I had to export a bunch of data in a CSV file. Turned out that at the receiving end the Perl script didn't want to eat my export. Luckily there is a standard for CSV files here and as strong Microsoft follower I adhere to the standards. Read up on RFC 4180 here

To make life a little easier in getting all fields of my class String.Formatted the correct way I hammered down a CustomFormatter to format output according RFC4180. Your WriteLines can now look like this where the CsvFormatter takes care of adding double-quotes for the fields that matter:

<snip>
// boring code omitted
 sww.WriteLine(
       String.Format(
                           new CsvFormatter(),
                           @"{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}",
                            topic.id,
                            topic.title,
                            topic.description,
                            topic.createdBy,
                            topic.createdOn,
                            topic.lastModifiedBy,
                            topic.lastModifiedOn,
                            postitem.id,
                            postitem.title,
                            postitem.content,
                            postitem.createdByUserName,
                            postitem.createdDate,
                            postitem.replyToId)
                            );

</snip> 

The code for the CsvFormatter is here:

public class CsvFormatter:IFormatProvider, ICustomFormatter
    {
        #region IFormatProvider Members
       public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter))
            {
                return this;
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region ICustomFormatter Members

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (arg == null)
            {
                return "";
            }
            string result = "";
            if (format == null)
            {
                result = arg.ToString();
            }
            else
            {
                result = String.Format(format, arg); // this part is never tested...
            }
            char[] escapeChars = new char[] {'"','\n','\r', ',' };
            if (result.IndexOfAny(escapeChars) > -1)
            {
                result = result.Replace("\"", "\"\"");
                result = "\"" + result + "\"";
            }
           
            return result;
        }

        #endregion
    }

 Obvious more ingenious ways are possible especially if you need more flexibility but this one did the trick for me (and Google didn't bring me a quick answer). 

Published Sun, Sep 6 2009 9:12 PM by Rene Schrieken

Comments

# re: CSV export

Is this your idea of a holiday ?

Tuesday, September 08, 2009 12:09 PM by pdv

# re: CSV export

Yes, What is wrong with it? I didn't use much of my 'programming' capabilities last year...

Thursday, September 17, 2009 10:06 PM by Rene Schrieken

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 7 and 1 and type the answer here: