0

我有 2 个工作簿视图:

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

如何将公式插入到 wbw2 中的工作表中,这些公式引用 wbw1 中工作表中的单元格?

4

1 回答 1

3

在WorkbookView对象之间创建单元格引用的技巧是确保两个 WorkbookView 共享相同的底层IWorkbookSet对象。IWorkbookSet 有点类似于 Excel 的运行实例,您可以在该实例中打开任意数量的工作簿,并且可以创建跨工作簿引用。

因此,如果您的所有 WorkbookView 对象共享相同的ActiveWorkbookSet,那么创建跨工作表或跨工作簿引用应该像在一个 WorkbookView 中的单元格中输入“=”符号然后单击另一个 WorkbookView 中的单元格一样简单。您应该会发现相应的引用已自动创建。例子:

// 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";
于 2013-10-31T20:20:10.560 回答