1

I have a cell with a content. I want to remove part of this content but with persistence style format. I tried to use function Substring(), but it doesn't save style format

For example:

My cell:

enter image description here

Want to see:

enter image description here

var firstDot = getCellValue.ToString().IndexOf(".");
var strWithoutBold = ws.Cell(1, 1).Value.ToString().Substring(firstDot);
Anthony14
  • 105
  • 6

1 Answers1

1

You need to use the RichText property of the cell. First copy the the shortened text from the cell, then clear the cell and insert the remaining parts of RichText:

int firstDot = ws.Cell(1, 1).GetString().IndexOf(".");
IXLFormattedText<IXLRichText> strWithoutBold =  ws.Cell(1, 1).RichText.Substring(firstDot);
ws.Cell(1, 1).RichText.ClearText();
foreach (IXLRichString rt in strWithoutBold)
{
    ws.Cell(1, 1).RichText.AddText(rt.Text).CopyFont(rt);
}

PS1: when you use the index of the dot, you keep the dot (which is bold). You may need to +1 the index.

PS2: Similar to your previous question, this will only work with ClosedXML versions up to 0.92. Maybe I will have a look at the library and try to make a simpler solution possibly.

Raidri
  • 17,258
  • 9
  • 62
  • 65