Search code examples
c#servicestackservicestack-text

Force Servicestack to delimit fields when producing CSV


I'm using Servicestack to produce CSV. The data contains mobile (cell) phone numbers. These start with a leading zero e.g. 04053333888. My problem is the consumers open this file in Excel, which truncates the leading zero. They then report this as a bug.

How can I force Servicestack to delimit the values? Preferably only the fields containing phone numbers would have delimiters, but I'm OK forcing all values to be delimited. I know how to set a custom delimiter; I'm happy to use the default double quote ("). If I embed a double quote in the source data Servicestack escapes them and I end up with """04053333888""" as the output. This is undesirable.


Solution

  • That behavior isn't configurable, but you can just replace the escaped quotes with a single quote to remove the escaping, e.g:

    var csv = records.ToCsv().Replace("\"\"\"","\"")