gpt4 book ai didi

c# - 使用 Excel Interop c# 将单词替换为粗体字

转载 作者:行者123 更新时间:2023-11-30 22:29:03 24 4
gpt4 key购买 nike

我正在使用以下搜索整个 Excel 工作表并将“req”替换为“requirement”:

oSheet.Cells.Replace("req", "requirement");

我不想替换单词 req,而是将其加粗。我怎样才能做到这一点?我知道这是错误的,但理论上我想执行以下操作:

oSheet.Cells.Replace("req", "<b>req</b>");

谢谢。

最佳答案

我假设您想将单元格中的个别文本项设置为粗体,这比第一次出现的要复杂一些。以下将达到目的:

    public void FindTextAndSetToBold(string text)
{
Excel.Range currentFind = null;
Excel.Range firstFind = null;

// Find the first occurrence of the passed-in text

currentFind = oSheet.Cells.Find(text, Missing.Value, Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext,
false, Missing.Value, Missing.Value);

while (currentFind != null)
{
// Keep track of the first range we find

if (firstFind == null)
{
firstFind = currentFind;
}
else if (currentFind.get_Address(Missing.Value, Missing.Value, Excel.XlReferenceStyle.xlA1,
Missing.Value, Missing.Value) ==
firstFind.get_Address(Missing.Value, Missing.Value, Excel.XlReferenceStyle.xlA1,
Missing.Value, Missing.Value))
{
// We didn't move to a new range so we're done

break;
}

// We know our text is in first cell of this range, so we need to narrow down its position

string searchResult = currentFind.get_Range("A1").Value2.ToString();
int startPos = searchResult.IndexOf(text);

// Set the text in the cell to bold

currentFind.get_Range("A1").Characters[startPos + 1, text.Length].Font.Bold = true;

// Move to the next find

currentFind = oSheet.Cells.FindNext(currentFind);
}
}

部分摘自here并修改。

关于c# - 使用 Excel Interop c# 将单词替换为粗体字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10392494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com