Add line break in spreadsheet cell

1 Answer 1583 Views
SpreadProcessing SpreadStreamProcessing
Jon
Top achievements
Rank 1
Jon asked on 25 Mar 2022, 04:00 PM
Is there a way to add a carriage return\line feed (like an Alt+Enter) in an Excel cell value?

1 Answer, 1 is accepted

Sort by
2
Svilen
Telerik team
answered on 28 Mar 2022, 10:28 AM

Hi, Jon,

Svilen here, I will be glad to help out with this question.

You can add new lines using \n and \r as you usually do in C#. You might notice, however, that if you simply do that the cell text in the output file still shows on one line until you double-click the cell. This is due to the fact that using "Alt+Enter" in Excel not only adds a new line but also automatically wraps the text. This helps visualize the new line and can manually be done by selecting a cell then checking the "Format Cells > Alignment tab > Wrap text" checkbox in Excel.

You can mirror this in SpreadProcessing by setting the cell's IsWrapped property to true. Here is a short code snippet, which does this:

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add(); 

string text = "line 1 " + "\r" + "line 2" + "\n" + "line 3";
worksheet.Cells[0, 0].SetValue(text);
worksheet.Cells[0, 0].SetIsWrapped(true);

You can read more about styles in our Get, Set, and Clear Cell Properties.

Please let me know if I can further help and have a great day.

Regards,
Svilen
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
SpreadProcessing SpreadStreamProcessing
Asked by
Jon
Top achievements
Rank 1
Answers by
Svilen
Telerik team
Share this question
or