Break row into table cell in CSV format to be read by Excel

Asked

Viewed 6,360 times

2

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

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

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

Here is the full 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();
  • Use the ALT+Enter key escape

  • What is the context of the code presented? This seems to me a request being processed on the server... how could this stop in an excel table? Add more details on the question.

  • I agree with @Miguelangelo, this scenario presented seems that you are opening a text file in excel with a contenttype = "application/excel". This way I suggest you create a <table> break the text inside the <TD> and display as excel content.

  • I edited the question with the context

4 answers

2

Considering the new context presented, to solve the problem should put double quotes " indicating the text that will have multiple lines, ie:

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

Reference: CSV with multi-line cells (English)

0

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

context.Response.Write("<table>" + 
                            "<tr>" + 
                                 "<td>aa<br/>bb</br>cc</td>" + 
                            "</tr>" + 
                       "</table>");

0

Suffice save an Excel table in CVS format, with a line return inside the cell.

I did it in excel 2007 and what he saved was a '\n'. Also, you put the value of the cell inside double quotes.

Example in C#, of how to represent the cell value:

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

0

Fields with embedded line breaks should be placed inside double quotes.

context.Response.Write("2013;Chevrolet;Cruze;\"linha1"+ "\r\n"+"linha2\"")
  • Fields with embedded line breaks should be placed inside double quotes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.