How to write data on multiple lines BUT within the

2020-02-09 07:17发布

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?

标签: c# csv
6条回答
疯言疯语
2楼-- · 2020-02-09 07:33

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

查看更多
混吃等死
3楼-- · 2020-02-09 07:33

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

A CSV file is defined as one row PER LINE, with cells separated by comma (","). Now, your parser could support using "" to put together sentences in one cell, but that already stretches the specification. You could also encode the \n and decode this later, but then again this is not a vcsv file. At the end, you try to do something a csv file is per definition not defined to do. \n IS the new line symbol. NATURALLY an editor will show a new line here.

查看更多
该账号已被封号
4楼-- · 2020-02-09 07:46
string input = "...";

var r = input.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries) // split by dot (have you any better approaches?)
             .Select(s => String.Format("\"{0}.\"", s.Trim())); // escape each with quotes

input = String.Join(Environment.NewLine, r); // break by line break
查看更多
够拽才男人
5楼-- · 2020-02-09 07:54

You might be able to change which tokens your application uses to parse rows from the csv file, but according to the definition of the csv file format, lines in the file are used to represent rows in the resulting tabular data.

查看更多
女痞
6楼-- · 2020-02-09 07:54

CSV stands for "comma seperated values" which is just a description of a text-file. If you want to import into Excel and then only have the sentences in one line you should try:

Environment.NewLine

instread of a simple /n

查看更多
放荡不羁爱自由
7楼-- · 2020-02-09 07:56

To quote Wikipedia:

Fields with embedded line breaks must be enclosed within double-quote characters.

Like e.g.:

1997,Ford,E350,"Go get one now
they are going fast"
查看更多
登录 后发表回答