How to get LineBreaks in a single Cell in EPPlus with RichText
Asked Answered
R

3

8

I use the following Code:

using (var package = new ExcelPackage()) {    
    var worksheet = package.Workbook.Worksheets.Add("Test");    
    var cell = worksheet.Cells[1, 1];

    var r1 = cell.RichText.Add("TextLine1" + "\r\n");
    r1.Bold = true;
    var r2 = cell.RichText.Add("TextLine2" + "\r\n");
    r2.Bold = false;

    package.SaveAs(...);
}

But in the Excel file the newLines are gone...

I tried also with "\n" and "\r" but nothing was working...

Riffle answered 14/3, 2018 at 17:15 Comment(2)
I think line breaks in openxml are <br> Here's a similar question for Word #2872387Helladic
OK, but how would the Code look like? If I just replace the "\r\n" with <br> the <br> appears as normal Text in the cell...Riffle
R
9

Finally I found the solution. Here is a working sample:

    using (var package = new ExcelPackage(fileInfo)) {    
         var worksheet = package.Workbook.Worksheets.Add("Test");    
         var cell = worksheet.Cells[1, 1];
         cell.Style.WrapText = true;
         cell.Style.VerticalAlignment = ExcelVerticalAlignment.Top;

         var r1 = cell.RichText.Add("TextLine1" + "\r\n");
         r1.Bold = true;
         var r2 = cell.RichText.Add("TextLine2" + "\r\n");
         r2.Bold = false;

         package.Save();
     }

But I think I found a bug in the Lib: This Code is NOT working:

    using (var package = new ExcelPackage(fileInfo)) {    
         var worksheet = package.Workbook.Worksheets.Add("Test");    
         var cell = worksheet.Cells[1, 1];
         cell.Style.WrapText = true;
         cell.Style.VerticalAlignment = ExcelVerticalAlignment.Top;

         var r1 = cell.RichText.Add("TextLine1" + "\r\n");
         r1.Bold = true;
         var r2 = cell.RichText.Add("TextLine2" + "\r\n");
         r2.Bold = false;

         cell = worksheet.Cells[1, 1];
         var r4 = cell.RichText.Add("TextLine3" + "\r\n");
         r4.Bold = true;

         package.Save();
     }

When I get the same range again and add new RichText Tokens, the old LineBreaks are deleted... (They are actually converted to "\n" and this is not working in Excel.)

Riffle answered 14/3, 2018 at 19:21 Comment(0)
B
4

The Encoding of new line in excel cell is 10. I think you have to do someihing like thise:

var r1 = cell.RichText.Add("TextLine1" + ((char)10).ToString());
Bauxite answered 21/3, 2018 at 16:2 Comment(0)
I
0
 using (var package = new ExcelPackage(fileInfo)) {    
         var worksheet = package.Workbook.Worksheets.Add("Test");    
         var cell = worksheet.Cells[1, 1];
         cell.Style.WrapText = true;
         cell.Style.VerticalAlignment = ExcelVerticalAlignment.Top;
         var r1 = cell.RichText.Add("TextLine1" + "\n\n");
         r1.Bold = true;
         var r2 = cell.RichText.Add("TextLine2" + "\n\n");
         r2.Bold = false;
         package.Save();
     }
Indefeasible answered 15/12, 2022 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.