How to write data on multiple lines BUT within the same cell of csv?

Viewed 43238

I want to create one csv file using C#.

I have some data which I want to write on multiple lines BUT within the same cell.

For example If I have following three sentences,

Sample sentence 1. This is second sample sentence. and this is third sentence.

I want to write all these three sentences within single cell of csv file but I want three of them on separate line.

My expected output is :

Sample sentence 1.
This is second sample sentence.
and this is third sentence.

Currently I am trying to achieve this by using \n character between two sentences but when I do it this way, all three sentences go on separate row.

Can anyone please tell me how to solve this problem?

6 Answers

Always wrap your description in between "\" eg: CsvRow.Add("\"" + ID + " : " + description + "\"");

string filePath = @"d:\pandey.csv";
        StringBuilder outString=new StringBuilder();
        outString.Append("\""); //double quote 
        outString.Append("Line 1, Hi I am line 1 of same cell\n");
        outString.Append("Line 2 Hi I am line 2 of same cell\n");
        outString.Append("Line 3 Hi I am line 3 of same cell\n");
        outString.Append("Line 4 Hi I am line 4 of same cell");
        outString.Append("\""); //double quote             
        File.WriteAllText(filePath, outString.ToString());
Related