Back to Devexpress

WorksheetCopyOptions.CopyDefinedNames Property

officefileapi-devexpress-dot-spreadsheet-dot-worksheetcopyoptions-2df8aa6b.md

latest3.1 KB
Original Source

WorksheetCopyOptions.CopyDefinedNames Property

Specifies which defined names should be copied in case they contain references to non-existent worksheets.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public CopyDefinedNamesMode CopyDefinedNames { get; set; }
vb
Public Property CopyDefinedNames As CopyDefinedNamesMode

Property Value

TypeDescription
CopyDefinedNamesMode

An enumeration value that indicates the defined name type to copy.

|

Available values:

NameDescription
All

Copy all defined names.

| | Referenced |

Copy defined names that reference existing worksheets.

|

Remarks

When you copy a worksheet from one workbook to another, the source worksheet may have defined names. These names might reference worksheets that do not exist in the target workbook. Use the CopyDefinedNames property to specify whether to copy these defined names to the target workbook. Set the property to Referenced to exclude references to non-existent worksheets.

The following code snippet specifies copy options and copies a worksheet between workbooks:

csharp
using DevExpress.Spreadsheet;
//...

using (Workbook sourceWorkbook = new Workbook())
using (Workbook targetWorkbook = new Workbook())
{
    targetWorkbook.LoadDocument(@"Documents\Book1.xlsx");
    sourceWorkbook.LoadDocument(@"Documents\Book2.xlsx");

    var copyOptions = new WorksheetCopyOptions();
    copyOptions.CopyDefinedNames = CopyDefinedNamesMode.Referenced;

    targetWorkbook.Worksheets[0].CopyFrom(sourceWorkbook.Worksheets[0], copyOptions);
    targetWorkbook.Calculate();
    targetWorkbook.SaveDocument("MergedDocument.xlsx");
}
vb
Imports DevExpress.Spreadsheet

Sub Main()
    Using sourceWorkbook As New Workbook()
        Using targetWorkbook As New Workbook()
            targetWorkbook.LoadDocument("Documents\Book1.xlsx")
            sourceWorkbook.LoadDocument("Documents\Book2.xlsx")

            Dim copyOptions As New WorksheetCopyOptions()
            copyOptions.CopyDefinedNames = CopyDefinedNamesMode.Referenced

            targetWorkbook.Worksheets(0).CopyFrom(sourceWorkbook.Worksheets(0), copyOptions)
            targetWorkbook.Calculate()
            targetWorkbook.SaveDocument("MergedDocument.xlsx")
        End Using
    End Using
End Sub

See Also

WorksheetCopyOptions Class

WorksheetCopyOptions Members

DevExpress.Spreadsheet Namespace