0

I'd like to subclass the NetOffice.ExcelApi.Worksheet class because I need a "custom" worksheet with the extra fields. As part of the construction of an instance of this new class, I'd like to just call the base class' constructor. That is, basically I need a regular Worksheet but need to set a few extra fields and have a couple of extra functions on it.

How do I create and new Worksheet? So far, I've only used the Add method from the Sheets class, which automatically does this for you and then adds it to the sheets. As far as I can tell, the constructor of the the Worksheet class needs to get the parent object (which is not a problem to get) and a COM proxy passed in. How do I do this?

siki
  • 9,077
  • 3
  • 27
  • 36

2 Answers2

0

I recommend you to use NPOI for working with excel. It works fine, not requires installed excel. It is available from NuGet. Samples are here. Hope this help.

Alex Erygin
  • 3,161
  • 1
  • 22
  • 22
  • Unfortunately, that's not an option. I'm writing an Excel add-in and need to use NetOffice. – siki Mar 18 '15 at 21:06
0

First of it all: i try to answer on codeplex for few days but i got a system degradation notice. (codeplex is currently down on dicussions. full discussion: http://netoffice.codeplex.com/discussions/587938)

Not sure i understand you correctly or may you didnt realize how COM is working. Any time you called Application.ActiveSheet you got a new!!! instance(proxy from the application) Of course its still the same instance on the COM-Server(if current sheet not changed) but you got a new instance/proxy each time you ask for. (You dont need to dispose each instance/proxy instance in NetOffice explictitly, moreover you can compare the local proxy-instances as easy with "==" or "!=" but this is just some NetOffice magic)

You have to re-cast a Worksheet instance each time you ask for or use cache strategy.

What i can do for you is to spend an event in the NetOffice Factory Core. Something like this:

//Event Trigger Example
private NetOfficeCore_OnCreateInstance(object newInstance, ref Type replaceTo)
{
      Worksheet sheet = newInstance as WorkSheet;
      if(null != sheet && sheet.Name = "MySheet")
      {
            replaceTo = typeof(MyCustomWorkSheet);
      }
}

In this example, all sheets with name "MySheet" want replaced automicaly with your custom type. (You still have to convert Application.ActiveSheet to your custom type of course)

Let me know what you think* so i can create this event today and build a new release.

*Sebastian (Disclaimer: Author from NetOffice)