无法将透明代理转换为类型“Microsoft.Office.Interop.Excel.Range”

发布于 2025-01-14 04:52:38 字数 522 浏览 2 评论 0原文

在极少数情况下,我会收到错误:System.InvalidCastException:无法将透明代理转换为类型“Microsoft.Office.Interop.Excel.Range”。我不知道如何处理/解决这个问题。它随机发生,而不是在同一行。当我尝试编写某些内容、制作边框和其他内容时,我总是投射 ((MyExcel.Range)Excel.Cells..).Value = ..,有时它只是一个字符串、数字、数组、边框,或尝试插入新行。下面的代码只是一个示例。

using MyExcel = Microsoft.Office.Interop.Excel;
...

private void xx()
try
{
    MyExcel.Application Excel = Globals.ThisAddIn.Application;
    ((MyExcel.Range)Excel.Cells[5, 5]).Value = aaa1;
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

In very rare scenarios I get error: System.InvalidCastException: Unable to cast transparent proxy to type 'Microsoft.Office.Interop.Excel.Range'. And I'm not sure how to deal/fix this. It occurs randomly, not on same row. When I try something to write, make borders and other stuff I always cast ((MyExcel.Range)Excel.Cells..).Value = .., sometimes its just a string, numbers, arrays, borders, or trying insert new rows. The Code below it's just a sample.

using MyExcel = Microsoft.Office.Interop.Excel;
...

private void xx()
try
{
    MyExcel.Application Excel = Globals.ThisAddIn.Application;
    ((MyExcel.Range)Excel.Cells[5, 5]).Value = aaa1;
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2025-01-21 04:52:38

首先,您需要打破属性和方法调用链:

((MyExcel.Range)Excel.Cells[5, 5]).Value = aaa1;

首先,按以下方式声明范围对象:

MyExcel.Range range = Excel.Cells[5, 5] as MyExcel.Range;

现在,如果 range 对象不是 null 您可以尝试调用Value属性。

First of all, you need to break the chain of property and method calls:

((MyExcel.Range)Excel.Cells[5, 5]).Value = aaa1;

First, declare the range object in the following way:

MyExcel.Range range = Excel.Cells[5, 5] as MyExcel.Range;

Now if the range object is not null you can try to call the Value property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文