Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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互操作无法获取字符。范围长度为>;时的文本属性;256_C#_Excel_Interop - Fatal编程技术网

C#Excel互操作无法获取字符。范围长度为>;时的文本属性;256

C#Excel互操作无法获取字符。范围长度为>;时的文本属性;256,c#,excel,interop,C#,Excel,Interop,我试图使用C#Excel互操作库在Excel工作表中加粗特定字符。我在大括号{}内有特定的字符,并使用此函数将其加粗 void FormatCharacters(Range r) { Characters chars = r.Characters[0, r.Text.Length]; if (chars.Text.Contains("{") && chars.Text.Contains("}")) { int startIndex = ch

我试图使用C#Excel互操作库在Excel工作表中加粗特定字符。我在大括号{}内有特定的字符,并使用此函数将其加粗

void FormatCharacters(Range r)
{
    Characters chars = r.Characters[0, r.Text.Length];
    if (chars.Text.Contains("{") && chars.Text.Contains("}"))
    {
        int startIndex = chars.Text.IndexOf("{");
        int len = chars.Text.IndexOf("}") - startIndex + 1;
        Characters bold = r.Characters[startIndex, len];
        bold.Font.Bold = true;
        bold.Text = bold.Text.Replace("{", "");
        bold.Text = bold.Text.Replace("}", "");
    }
    else
        return;
    FormatCharacters(r);
}
如果单元格的文本少于256个字符,则该函数可以正常工作。如果单元格的文本大于256个字符,则会出现以下异常:

System.Runtime.InteropServices.COMException

Message=无法获取Characters类的Text属性

错误代码=-2146827284

如何在Excel字符界面中克服256个字符的限制?


是否有其他使用互操作的方法?

我很确定这不是您的代码的问题,而是Excel互操作库的一个更大的限制。不确定这是有意的限制,但您可以将它们分为256个块进行处理,以使其正常工作。我尝试过分块处理它们,这时我意识到限制不是我们选择的字符大小,而是范围长度本身。我尝试过的其他方法是避免使用Characters.Text属性,并使用range.replace函数替换“{',“}”字符。但是当我这样做时,我会丢失整个单元格中的格式!!