0

I use closedXml and I want to concatenate cells which located in one row.

In column E/F/G is what I have. And in column J is that I want to see:

In addition, I want to save bold/italic format

enter image description here

Can anyone help me?

Community
  • 1
  • 1
Anthony14
  • 105
  • 6

1 Answers1

1

A little more complicated than I expected it to be but still relative straightforward.
Copy each RichText part from the three source cells to the target cell:

XLWorkbook wb = new XLWorkbook(@"c:\temp.xlsx");
IXLWorksheet worksheet = wb.Worksheet(1);

foreach (IXLRow row in worksheet.RowsUsed())
{
    row.Cell("J").RichText.ClearText();
    foreach (var rt in row.Cell("E").RichText)
    {
        row.Cell("J").RichText.AddText(rt.Text).CopyFont(rt);
    }
    row.Cell("J").RichText.AddText(" ");
    foreach (var rt in row.Cell("F").RichText)
    {
        row.Cell("J").RichText.AddText(rt.Text).CopyFont(rt);
    }
    row.Cell("J").RichText.AddText(" ");
    foreach (var rt in row.Cell("G").RichText)
    {
        row.Cell("J").RichText.AddText(rt.Text).CopyFont(rt);
    }
}

wb.Save();
Raidri
  • 17,258
  • 9
  • 62
  • 65
  • Nice, but I have a question: what does it mean 'CopyFont'? Because when I try to use this method, I have an error: 'IXLRichString' does not contain a definition for CopyFont... How I should use it? – Anthony14 Jul 05 '19 at 06:08
  • `CopyFont` copies the font and styling (bold, italic). Which version of ClosedXML do you use? – Raidri Jul 05 '19 at 07:20
  • Latest stable 0.94.2 – Anthony14 Jul 05 '19 at 07:24
  • I changed version to 0.91.0 and it works right! – Anthony14 Jul 05 '19 at 07:36
  • `CopyFont` is an extension method. All CloseXML extension methods were made internal in version 0.93, so it should work high 0.92 or lower. If you need it in a higher version, you can copy the source code from [here](https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Extensions.cs#L220). – Raidri Jul 05 '19 at 09:28