简体   繁体   中英

How to remove part of string and save style format

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:

在此处输入图片说明

Want to see:

在此处输入图片说明

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM