Skip to main content

How to: Copy Worksheets

  • 3 minutes to read

Important

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

Use the Worksheet.CopyFrom method to copy data from one worksheet to another.

Copy Worksheets Within a Workbook

using DevExpress.Spreadsheet;
// ...

using (Workbook workbook = new Workbook())
{
    // Add a new worksheet to the workbook.
    workbook.Worksheets.Add("Sheet1_Copy");

    // Copy all information from "Sheet1" 
    // to the newly created worksheet.
    workbook.Worksheets["Sheet1_Copy"].CopyFrom(workbook.Worksheets["Sheet1"]);
}

Copy Worksheets Between Workbooks

using DevExpress.Spreadsheet;
// ...

using (Workbook sourceWorkbook = new Workbook())
using (Workbook targetWorkbook = new Workbook())
{
    // Add data to the first worksheet of the source workbook.
    sourceWorkbook.Worksheets[0].Cells["A1"].Value = "A worksheet to copy";
    sourceWorkbook.Worksheets[0].Cells["A1"].Font.Color = Color.ForestGreen;

    // Copy the first worksheet of the source workbook 
    // to the destination workbook.
    targetWorkbook.Worksheets[0].CopyFrom(sourceWorkbook.Worksheets[0]);
}

Specify Copy Options

Pass a WorksheetCopyOptions instance to the Worksheet.CopyFrom 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.

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);
    }
}