Domanda

I have 2 WorkbookViews:

<sprgr:WorkbookView Name="wbw1"/>
<sprgr:WorkbookView Name="wbw2"/>

How to insert formulas to a Worksheet in wbw2 that refer to cells in a Worksheet in wbw1?

È stato utile?

Soluzione

The trick to creating cell references between WorkbookView objects is to ensure that both WorkbookViews share the same underlying IWorkbookSet object. An IWorkbookSet is sort of analogous to a running instance of Excel, where you can have any number of workbooks opened within that instance, and you can create cross-workbook references.

So if all of your WorkbookView objects share the same ActiveWorkbookSet, then creating cross-sheet or cross-workbook references should be as simple as entering an "=" sign in a cell in one WorkbookView and then clicking on a cell in the other WorkbookView. You should find the appropriate reference is automatically created. Example:

// Create a single workbook set containing two workbooks
IWorkbookSet wbs = Factory.GetWorkbookSet();
IWorkbook workbook1 = wbs.Workbooks.Add();  // Name = Book1
IWorkbook workbook2 = wbs.Workbooks.Add();  // Name = Book2

// Associate each workbook to a WorkbookView.  Because workbook1
// and workbook2 share the same underlying IWorkbookSet, wbw1 and wbw2
// now also share the same ActiveWorkbookSet, allowing for cross-workbook
// cell references
wbw1.ActiveWorkbook = workbook1;
wbw2.ActiveWorkbook = workbook2;

// Have user press "=" in a cell on one WorkbookView and then
// select a cell in the other WorkbookView.  This should result
// in a cross-workbook reference being created.

// Programmatically, you could just set a cell formula in wbw1
// to reference a cell in the other workbook.
wbw1.ActiveCell.Formula = "=[Book2]Sheet1!$A$1";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top