Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+
Row

WorksheetCopyOptions Class

Allows you to define copy options when you copy data from one worksheet to another.

Namespace: DevExpress.Spreadsheet

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

Declaration

public class WorksheetCopyOptions

Remarks

Use the Worksheet.CopyFrom method copy data from one worksheet to another. Create a WorksheetCopyOptions instance and pass it to this method to specify copy options. The following options are available:

Option Description
PasteOptions Specifies the part of data to paste from the copied worksheet into the target worksheet.
InvalidFormulaReplacementMode Specifies how to replace copied formulas if they contain references to worksheets that do not exist in the destination workbook.
SheetMappings Allows you to specify mappings between worksheet names in the source and destination workbooks. Use this property to replace external cell references in formulas with references to sheets in the destination workbook.
OverwriteProtectionOnLockedWorksheet Specifies whether to apply cell protection options of the source worksheet to cells in the protected destination worksheet.

Example

The example below demonstrates how to copy all worksheets from one workbook to another. Sheets are copied with their original names. The SheetMappings property is used to update formula references so they point to worksheets in the destination workbook.

using DevExpress.Spreadsheet;
using System.Linq;
// ...

using (Workbook sourceWorkbook = new Workbook())
using (Workbook targetWorkbook = new Workbook())
{
    targetWorkbook.LoadDocument(@"Documents\Book1.xlsx");
    sourceWorkbook.LoadDocument(@"Documents\Book2.xlsx");
    var copyOptions = new WorksheetCopyOptions();
    // Specify mappings between worksheets in the source
    // and destination workbooks. Sheets are copied 
    // with their original names.
    copyOptions.SheetMappings = sourceWorkbook.Sheets.ToDictionary(
        sheet => sheet.Name, sheet => sheet.Name);
    // Copy all worksheets from the source workbook
    // to the target workbook.
    CopySheetsToWorkbook(sourceWorkbook, targetWorkbook, copyOptions);
    targetWorkbook.Calculate();
    targetWorkbook.SaveDocument("MergedDocument.xlsx");
}

// ...

private static void CopySheetsToWorkbook(Workbook sourceWorkbook, 
    Workbook targetWorkbook, WorksheetCopyOptions options)
{
    foreach (var sheet in sourceWorkbook.Worksheets)
    {
        string sheetName = sheet.Name;
        if (!targetWorkbook.Worksheets.Contains(sheetName))
            targetWorkbook.Worksheets.Add(sheetName);
        targetWorkbook.Worksheets[sheetName].CopyFrom(sheet, options);
    }
}

Inheritance

Object
WorksheetCopyOptions
See Also