【问题标题】:How to remove part of string and save style format如何删除部分字符串并保存样式格式
【发布时间】:2019-07-07 11:01:22
【问题描述】:

我有一个包含内容的单元格。我想删除此内容的一部分,但使用持久性样式格式。我尝试使用函数 Substring(),但它不保存样式格式

例如:

我的手机:

想看:

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

【问题讨论】:

  • 显示您的代码,以便我们看到您在做什么。
  • 是的,我加了,可以看到
  • Excel 是一个 zip 文件。请将扩展名更改为.zip,然后解压缩。在记事本中查看文件的内容以了解该单元格的存储方式,一旦您对 XML 格式有了更好的了解,它将有助于使用 ClosedXml 读取它。另外,查看getCellValue.Style 中返回的内容
  • 你能帮我写代码吗?我不擅长这个

标签: c# closedxml


【解决方案1】:

您需要使用单元格的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:与您的previous 问题类似,这仅适用于最高 0.92 的 ClosedXML 版本。也许我会看看这个库并尝试做一个更简单的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多