Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Excel电子表格单元格中设置文本格式_C#_Excel - Fatal编程技术网

C# 在Excel电子表格单元格中设置文本格式

C# 在Excel电子表格单元格中设置文本格式,c#,excel,C#,Excel,我需要修改的代码将创建一个excel电子表格,并在单元格中插入搜索结果。逻辑找到匹配项并复制代码中的整行。然后用替换逗号,不知道为什么,但这并不重要。我需要突出显示搜索结果中的特定单词,最好是粗体和红色。请参见下面的示例,搜索参数是COMPUTE,结果是设计的整行代码 )使用(PAD\u INDEX=OFF STATISTICS\u NORECOMPUTE=OFF IGNORE\u DUP\u KEY=OFF ALLOW\u ROW\u LOCKS=ON 我有一些将“上下文”变量拆分为字符串数组

我需要修改的代码将创建一个excel电子表格,并在单元格中插入搜索结果。逻辑找到匹配项并复制代码中的整行。然后用
替换逗号,不知道为什么,但这并不重要。我需要突出显示搜索结果中的特定单词,最好是粗体和红色。请参见下面的示例,搜索参数是COMPUTE,结果是设计的整行代码

)使用(PAD\u INDEX=OFF STATISTICS\u NORECOMPUTE=OFF IGNORE\u DUP\u KEY=OFF ALLOW\u ROW\u LOCKS=ON

我有一些将“上下文”变量拆分为字符串数组的基本代码,如果找到匹配项,它应该格式化单词-然而这就是我的挑战所在

我找到了大量关于如何格式化整个单元格或行的示例,但没有关于单元格内文本的示例。这在语法上是不可能的吗

            string[] wordList = context.Split(' ');
            StringBuilder reassembleContext = new StringBuilder();
            int i = 0;
            regexPattern = new Regex(pattern.ToString(CultureInfo.InvariantCulture));

            foreach (string word in wordList)
            {
                var matches = Regex.Matches(word, pattern.ToString(), RegexOptions.IgnoreCase);
                if (matches.Count > 0)
                {
                    wordList[i] = "<b>" + word + "</b>"; // Clearly this does not work...
                }

                reassembleContext.Append(wordList[i]);
                reassembleContext.Append(" ");
                i++;
            }

            context = reassembleContext.ToString();
string[]wordList=context.Split(“”);
StringBuilder Remolecontext=新的StringBuilder();
int i=0;
regexpatern=newregex(pattern.ToString(CultureInfo.InvariantCulture));
foreach(单词列表中的字符串单词)
{
var matches=Regex.matches(word,pattern.ToString(),RegexOptions.IgnoreCase);
如果(matches.Count>0)
{
wordList[i]=“word+”;//显然这不起作用。。。
}
Append(wordList[i]);
文本。追加(“”);
i++;
}
context=recomblecontext.ToString();

一种方法是将HTML放入剪贴板,然后粘贴:

    System.Windows.Forms.Clipboard.SetText("<html>a<b>b</b>c");
    worksheet.Range["A1"].PasteSpecial();

我对在Excel中使用
c#
编程一无所知,但在Excel本身中,只要单元格内容是文本字符串而不是公式的结果,您就可以在单元格中设置文本格式。在VBA中,您可以使用characters属性来确定哪些字符的格式不同。这是Interop.Excel还是OpenXML,或者其他什么其他..?它是Interop.Excel,但这是可以更改的。
    worksheet.Range["A1"].Characters[38, 7].Font.Bold = true;