0

将控件直接添加到工作表时,我不断收到异常。代码放置在表单内,而不是功能区的一部分。

这是我的代码:

// using TExcel = Microsoft.Office.Tools.Excel;
// MyGlobalsWrapper references the Globals object, which has been passed across an
// AppDomain barrier

Excel.Worksheet activeWorksheet = MyGlobalsWrapper.Application.ActiveSheet;
TExcel.Worksheet toolsSheet = MyGlobalsWrapper.Factory.GetVstoObject(activeWorksheet);
var range = activeWorksheet.Range["A1:F20"];

var button = new System.Windows.Forms.Button();
button.Text = "HELLO!";
button.Visible = true;

// Both of these fail with the same exception

// Add manually (as the Range object might be the problem
toolsSheet.Controls.AddControl(button,10,10,button.Width,button.Height,id);

// Add using a range (which is more ideal for my usage)
toolsSheet.Controls.AddControl(button,range ,id);

例外情况如下:

System.Runtime.Serialization.SerializationException occurred
  Message=Type 'Microsoft.Office.Tools.Excel.ControlCollectionImpl' in Assembly 'Microsoft.Office.Tools.Excel.Implementation, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
       at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
       at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
       at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
       at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
       at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
       at System.Runtime.Remoting.Channels.CrossAppDomainSerializer.SerializeMessageParts(ArrayList argsToSerialize)
       at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage..ctor(IMethodReturnMessage mrm)
       at System.Runtime.Remoting.Messaging.SmuggledMethodReturnMessage.SmuggleIfPossible(IMessage msg)
       at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoDispatch(Byte[] reqStmBuff, SmuggledMethodCallMessage smuggledMcm, SmuggledMethodReturnMessage& smuggledMrm)
       at System.Runtime.Remoting.Channels.CrossAppDomainSink.DoTransitionDispatchCallback(Object[] args)
    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at Microsoft.Office.Tools.Excel.Worksheet.get_Controls()

我的参考资料如下:

Microsoft.Office.Interop.Excel
   v12 - CLR:v1.1.4322

Microsoft.Office.Interop.Excel.Extensions
   v12 - CLR:2.0.50727

Microsoft.Office.Tools
Microsoft.Office.Tools.Common
Microsoft.Office.Tools.Common。 v4.0.Utilities
Microsoft.Office.Tools.Excel
Microsoft.Office.Tools.Excel.v4.0.Utilities
   v10 - CLR:4.0.30919

我曾尝试将其包装在 中System.Windows.Forms.UserControl,但我有同样的问题。

注意:我想使用 Windows 控件,因为我打算在工作表中嵌入一个用户控件,而不仅仅是一个按钮。我从一个简单的控制开始,然后开始工作。所以我需要一个使用 Windows 窗体控件(最好是 UserControl)而不是 Excel 框架提供的任何控件的解决方案。

4

1 回答 1

1

您是否在单独的 AppDomain 中运行此代码?

如果您是,那么 ControlsCollection 将需要跨 AppDomain 屏障进行编组。与大多数编组一样,这将通过序列化来实现。

是否可以在主 Excel AppDomain 中运行此代码?

于 2012-05-17T09:44:43.787 回答