WorkbookViews之间的SpreadsheetGear公式

时间:2013-10-30 07:54:27

标签: wpf formula spreadsheetgear

我有2个WorkbookViews:

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

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

1 个答案:

答案 0 :(得分: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";