Back to Devexpress

Workbook.ExternalWorkbooks Property

officefileapi-devexpress-dot-spreadsheet-dot-workbook-6f5b5a2e.md

latest5.7 KB
Original Source

Workbook.ExternalWorkbooks Property

Provides access to the collection of source workbooks used for creating external references in the current workbook.

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this property in production code.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Docs.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public ExternalWorkbookCollection ExternalWorkbooks { get; }
vb
Public ReadOnly Property ExternalWorkbooks As ExternalWorkbookCollection

Property Value

TypeDescription
ExternalWorkbookCollection

An ExternalWorkbookCollection object.

|

Remarks

To use cell references and defined names contained in another workbook, you should add a workbook to the ExternalWorkbooks collection.

The following code snippet creates a workbook, populates it with random data by importing a data table and adds to the collection of external workbooks.

View Example

csharp
Workbook externalWorkbook = new Workbook();
externalWorkbook.Options.Save.CurrentFileName = "ExternalDocument.xlsx";
// Check whether the external workbook is already referenced.
foreach (IWorkbook item in myWorkbook.ExternalWorkbooks)
{
    if (item.Options.Save.CurrentFileName == externalWorkbook.Options.Save.CurrentFileName)
        return;
}
externalWorkbook.Worksheets[0].Import(CreateDataTable(10), false, 0, 0);
externalWorkbook.SaveDocument("ExternalDocument.xlsx");
myWorkbook.ExternalWorkbooks.Add(externalWorkbook);
vb
Dim externalWorkbook As New Workbook()
externalWorkbook.Options.Save.CurrentFileName = "ExternalDocument.xlsx"
' Check whether the external workbook is already referenced.
For Each item As IWorkbook In myWorkbook.ExternalWorkbooks
    If item.Options.Save.CurrentFileName = externalWorkbook.Options.Save.CurrentFileName Then
        Return
    End If
Next item
externalWorkbook.Worksheets(0).Import(CreateDataTable(10), False, 0, 0)
externalWorkbook.SaveDocument("ExternalDocument.xlsx")
myWorkbook.ExternalWorkbooks.Add(externalWorkbook)

Subsequently you can reference this workbook from the current workbook, as illustrated in the code below.

View Example

csharp
if (myWorkbook.ExternalWorkbooks.Count == 0)
{
    return;
}
IWorkbook extWorkbook = (IWorkbook)myWorkbook.ExternalWorkbooks[0];
string extWorkbookName = extWorkbook.Options.Save.CurrentFileName;
string sFormula = String.Format("=[{0}]Sheet1!A1", extWorkbookName);
myWorkbook.Worksheets[0].Cells["A1"].Formula = sFormula;
myWorkbook.SaveDocument("Test.xlsx");
System.Diagnostics.Process.Start("Test.xlsx");
vb
If myWorkbook.ExternalWorkbooks.Count = 0 Then
    Return
End If
Dim extWorkbook As IWorkbook = DirectCast(myWorkbook.ExternalWorkbooks(0), IWorkbook)
Dim extWorkbookName As String = extWorkbook.Options.Save.CurrentFileName
Dim sFormula As String = String.Format("=[{0}]Sheet1!A1", extWorkbookName)
myWorkbook.Worksheets(0).Cells("A1").Formula = sFormula
myWorkbook.SaveDocument("Test.xlsx")
System.Diagnostics.Process.Start("Test.xlsx")

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ExternalWorkbooks property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

spreadsheet-document-api-external-workbook/CS/SpreadsheetApiExternalWorkbook/Program.cs#L17

csharp
// Check whether the external workbook is already referenced.
foreach (IWorkbook item in myWorkbook.ExternalWorkbooks)
{

spreadsheet-document-api-external-workbook/VB/SpreadsheetApiExternalWorkbook/Program.vb#L14

vb
' Check whether the external workbook is already referenced.
For Each item As IWorkbook In myWorkbook.ExternalWorkbooks
    If item.Options.Save.CurrentFileName = externalWorkbook.Options.Save.CurrentFileName Then

Implements

ExternalWorkbooks

See Also

Workbook Class

Workbook Members

DevExpress.Spreadsheet Namespace