如何删除部分字符串并保存样式格式
How to remove part of string and save style format
我有一个包含内容的单元格。我想删除部分内容,但使用持久性格式。我尝试使用函数 Substring(),但它不保存样式格式
例如:
我的手机:
想看:
var firstDot = getCellValue.ToString().IndexOf(".");
var strWithoutBold = ws.Cell(1, 1).Value.ToString().Substring(firstDot);
您需要使用单元格的RichText
属性。首先从单元格中复制缩短的文本,然后清除单元格并插入 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:当你使用点的索引时,你保留点(粗体)。您可能需要 +1
索引。
PS2:与您的 问题类似,这仅适用于最高 0.92 的 ClosedXML 版本。也许我会看看这个库,并尽可能地尝试做一个更简单的解决方案。
我有一个包含内容的单元格。我想删除部分内容,但使用持久性格式。我尝试使用函数 Substring(),但它不保存样式格式
例如:
我的手机:
想看:
var firstDot = getCellValue.ToString().IndexOf(".");
var strWithoutBold = ws.Cell(1, 1).Value.ToString().Substring(firstDot);
您需要使用单元格的RichText
属性。首先从单元格中复制缩短的文本,然后清除单元格并插入 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:当你使用点的索引时,你保留点(粗体)。您可能需要 +1
索引。
PS2:与您的