【发布时间】: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