Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert programmatically a new line in an Excel cell in C#?

Tags:

I'm using the Aspose library to create an Excel document. Somewhere in some cell I need to insert a new line between two parts of the text.

I tried "\r\n" but it doesn't work, just displays two square symbols in cell. I can however press Alt+Enter to create a new line in that same cell.

How do I insert a new line programmatically?

like image 770
User Avatar asked Jul 24 '09 09:07

User


People also ask

How do you create a new line in excel within a cell?

To start a new line of text or add spacing between lines or paragraphs of text in a worksheet cell, press Alt+Enter to insert a line break. Double-click the cell in which you want to insert a line break (or select the cell and then press F2).

What is the code for next line in Excel?

Try using the [Alt] + [Enter] key. This will create a new line feed in the cell.

How do I do a carriage return in a cell?

We will press the excel shortcut key ALT + ENTER to insert the carriage return character in excel cell. As we press the “ALT + ENTER” key, it pushes the content in front of the selected data to the new line by inserting a carriage return.


2 Answers

From the Aspose Cells forums: How to use new line char with in a cell?

After you supply text you should set the cell's IsTextWrapped style to true

worksheet.Cells[0, 0].Style.WrapText = true; 
like image 189
Ahmad Mageed Avatar answered Oct 31 '22 08:10

Ahmad Mageed


cell.Text = "your firstline<br style=\"mso-data-placement:same-cell;\">your secondline"; 

If you are getting the text from DB then:

cell.Text = textfromDB.Replace("\n", "<br style=\"mso-data-placement:same-cell;\">"); 
like image 20
Kiran Avatar answered Oct 31 '22 08:10

Kiran