【问题标题】:C# Excel Interop InvalidCastException when trying to change Shape.Fill.Backcolor尝试更改 Shape.Fill.Backcolor 时的 C# Excel 互操作 InvalidCastException
【发布时间】:2016-03-02 21:53:32
【问题描述】:

当我尝试在 Excel 工作表上更改通过 C# 创建的新形状的背景颜色时出现错误。我在下面添加了会产生错误的示例代码。以下代码在安装了 Office 2013 的旧 PC 上运行良好,但在客户端计算机或我的新 PC(Windows 10 Office 2016)上运行时,我在 Shape.getFill() 调用和其他一些电话也是如此。

所有抛出的调用似乎都与获取格式化对象有关(例如,它也发生在 Chart.PlotArea.Format.get_Line() 调用中。我进行的大多数其他 C# Excel 互操作调用都工作正常。例如,我可以创建形状和图表,从单元格中写入和读取但是当我尝试更改某些对象的格式时会发生此错误.当我在新笔记本电脑上编译并运行以下代码时,我在旧笔记本电脑上编译和运行时得到一个模糊的 InvalidCastException 它可以正常工作错误。在旧机器上编译和运行并不能在新机器上解决此问题。我尝试在新 PC 上安装 2010 PIA 也没有成功。任何帮助将不胜感激。

代码示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace TestOfficeError {
    static class Program {
        [STAThread]
        static void Main() {
            Excel.Application app = new Excel.Application();
            Excel.Workbooks wbs = app.Workbooks;
            Excel.Workbook wb = wbs.Add();
            Excel.Sheets sheets = wb.Sheets;
            Excel.Worksheet sheet = sheets[1];

            Excel.Shape shape = sheet.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 10, 10, 100, 100);

            //Exception thrown on this line. See exception below.
            shape.Fill.BackColor.RGB = 0;

            wb.Activate();

            SafeRelease(sheet);
            SafeRelease(sheets);
            SafeRelease(wb);
            SafeRelease(wbs);
            SafeRelease(app);
            FinalCleanup();

        }

        static void SafeRelease(object obj) {
            if (obj != null){
                Marshal.FinalReleaseComObject(obj);
            }
        }

        static void FinalCleanup() {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

异常

System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=Return argument has an invalid type.
  Source=mscorlib
  StackTrace:
       at System.Runtime.Remoting.Proxies.RealProxy.ValidateReturnArg(Object arg, Type paramType)
       at System.Runtime.Remoting.Proxies.RealProxy.PropagateOutParameters(IMessage msg, Object[] outArgs, Object returnValue)
       at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
       at Microsoft.Office.Interop.Excel.Shape.get_Fill()
       at TestOfficeError.Program.Main() in c:\users\chris\documents\visual studio 2015\Projects\TestOfficeError\TestOfficeError\Program.cs:line 28
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

【问题讨论】:

  • 你在过去的两年里有没有偶然解决过这个问题?我遇到了同样的错误,我不知道如何修复它或在代码中提出解决方法。

标签: c# excel exception interop


【解决方案1】:

我刚刚遇到了 Excel.Shape.Line 属性的类似问题。问题是用户安装了 64 位版本的 Office 2010。将其更改为 32 位版本即可解决问题。

【讨论】:

  • 不幸的是,我们都已经在运行 32 位版本的 Office。
  • 我有同样的问题,但我无法以任何方式更改客户的计算机。开发人员的解决方案是什么?
【解决方案2】:

我使用 dynamic 而不是 Excel.Shape 类型解决了这个问题。

这只是一种解决方法,但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多