Breaks row in table cell in CSV format to be read by Excel

2

I have the following code that inserts a value into an excel cell for export

context.Response.Write("aa\r\nbb\r\nccc");

o / r / n breaks the line but writes in the cell below, I would like to break the line but continue with the text in the same cell. How should I do it?

Here is the complete code

HttpContext context = HttpContext.Current;
context.Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
context.Response.Charset = "UTF-8";
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=teste.csv");

context.Response.Write("aa\r\nbb\r\nccc");
context.Response.End();
    
asked by anonymous 03.04.2014 / 17:41

4 answers

2

Considering the new context presented, to solve the problem, you should put double quotation marks " indicating the text that will have multiple lines, that is:

context.Response.Write("\"aa\rb\rccc\"");

Reference: CSV with multi-line cells (English)

    
03.04.2014 / 18:23
0

The presented scenario looks like you are generating the content in a WEB application. This way I suggest you create a <table> in HTML and break the text inside <TD> . and display as excel content. Try:

context.Response.Write("<table>" + 
                            "<tr>" + 
                                 "<td>aa<br/>bb</br>cc</td>" + 
                            "</tr>" + 
                       "</table>");
    
03.04.2014 / 17:52
0

Just Save an Excel table in CVS format , with a cell return inside the cell.

I did this in excel 2007 and what he saved was '\n' . Also, put the value of the cell inside double quotation marks.

Example in C #, how to represent cell value:

var celula = @"""a\nb"""; // conteúdo da célula com quebra de linha
    
03.04.2014 / 19:42
0

Fields with embedded line breaks should be enclosed in double quotation marks.

context.Response.Write("2013;Chevrolet;Cruze;\"linha1"+ "\r\n"+"linha2\"")
    
03.04.2014 / 23:20