user2083386
user2083386

Reputation: 135

Adding double quotes to string while writing to csv c#

I am trying to write data to csv file. I want the data to be displayed in double quotes when opened in notepad, but it shouldn't show any double quotes when I open the file in csv.

following is the code that I am using but, it doesn't give the result.

csvFile.WriteField("\"" +data.FirstName == null ? string.Empty : data.FirstName + "\"");

Can someone help me out with this?

Upvotes: 0

Views: 9005

Answers (3)

Jai
Jai

Reputation: 11

Using the CsvConfiguration, you can change the CSV file configuration. To add the double quotes set the ShouldQuote=true, and to remove set ShouldQuote=false.

Plz try this in the latest version, I tried it in V28, and its works for me

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                ShouldQuote = (field) => false //Set the false to remove the double quotes
            };
            using (StreamWriter _streamWriter = new StreamWriter(fileNamePath))
            {
                using (var _csvWriter = new CsvWriter(_streamWriter, config))
                {
                    await _csvWriter.WriteRecordsAsync(models);
                }
            }

Upvotes: 1

David Specht
David Specht

Reputation: 9104

The problem is you are trying to fight CsvHelper's quoting. According to the RFC 4180 specifications.

  1. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:
       "aaa","b CRLF
       bb","ccc" CRLF
       zzz,yyy,xxx
  1. If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:
       "aaa","b""bb","ccc"

So when you add double quotes to the field, CsvHelper recognizes that the whole field needs to be enclosed in double quotes and the added quotes need to be escaped with another double quote. That is why you end up with 3 double quotes.

CsvHelper has a configuration function where you can tell it when it should quote a field. @JoshClose already has an answer here for wrapping all fields in double quotes. I'll update it for the current version of CsvHelper.

void Main()
{
    var records = new List<Foo>
    {
        new Foo { Id = 1, Name = "one" },
        new Foo { Id = 2, Name = "two" },
    };

    using (var writer = new StringWriter())
    using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
    {
        csv.Configuration.ShouldQuote = (field, context) => true;
        csv.WriteRecords(records);

        writer.ToString().Dump();
    }
}

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

If you still wanted to add the double quotes yourself, you could turn off CsvHelper's double quoting.

csv.Configuration.ShouldQuote = (field, context) => false;

Breaking change for Version 20.0.0 and later

Changed CsvConfiguration to a read only record to eliminate threading issues.

You would need to create CsvConfiguration, set ShouldQuote on initialization and then pass it to the CsvWriter.

void Main()
{
    var records = new List<Foo>
    {
        new Foo { Id = 1, Name = "one" },
        new Foo { Id = 2, Name = "two" },
    };
    
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        ShouldQuote = (field, context) => true
    };

    using (var writer = new StringWriter())
    using (var csv = new CsvWriter(writer, config))
    {
        csv.WriteRecords(records);

        writer.ToString().Dump();
    }
}

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Upvotes: 4

Krystian Sitek
Krystian Sitek

Reputation: 588

I made code a bit more readable by placing the assignment of firstName to a variable that will be passed later. I used a string escape to add a quote to the final result. Now you can pass it to your WriteField method.

More information about string escape can be found: here and here.

var firstName = data.FirstName == null ? string.Empty : data.FirstName;
var firstNameWithQuoutes = $@"""{firstName}""";

Upvotes: 0

Related Questions