Back to Devexpress

How to: Copy Worksheets

officefileapi-12082-spreadsheet-document-api-examples-worksheets-how-to-copy-worksheets.md

latest7.0 KB
Original Source

How to: Copy Worksheets

  • Sep 17, 2025
  • 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

csharp
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"]);
}
vb
Imports DevExpress.Spreadsheet
' ...

Using workbook As 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"))
End Using

Copy Worksheets Between Workbooks

csharp
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]);
}
vb
Imports DevExpress.Spreadsheet
' ...

Using sourceWorkbook As New Workbook()
Using targetWorkbook As 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))
End Using
End Using

Specify Copy Options

Pass a WorksheetCopyOptions instance to the Worksheet.CopyFrom method to specify copy options. The following options are available:

OptionDescription
PasteOptionsSpecifies the part of data to paste from the copied worksheet into the target worksheet.
InvalidFormulaReplacementModeSpecifies how to replace copied formulas if they contain references to worksheets that do not exist in the destination workbook.
SheetMappingsAllows 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.
OverwriteProtectionOnLockedWorksheetSpecifies whether to apply cell protection options of the source worksheet to cells in the protected destination worksheet.
CopyDefinedNamesSpecifies which defined names should be copied in case they contain references to non-existent worksheets.

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.

csharp
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);
    }
}
vb
Imports DevExpress.Spreadsheet
Imports System.Linq
' ...

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()
    ' Specify mappings between worksheets in the source
    ' and destination workbooks. Sheets are copied 
    ' with their original names.
    copyOptions.SheetMappings = sourceWorkbook.Sheets.ToDictionary(
      Function(sheet) sheet.Name, Function(sheet) sheet.Name)
    ' Copy all worksheets from the source workbook
    ' to the target workbook.
    CopySheetsToWorkbook(sourceWorkbook, targetWorkbook, copyOptions)
    targetWorkbook.Calculate()
    targetWorkbook.SaveDocument("MergedDocument.xlsx")
End Using
End Using

' ...

Private Sub CopySheetsToWorkbook(sourceWorkbook As Workbook, targetWorkbook As Workbook,
                                 options As WorksheetCopyOptions)
    For Each sheet In sourceWorkbook.Worksheets
        Dim sheetName As String = sheet.Name
        If Not targetWorkbook.Worksheets.Contains(sheetName) Then
            targetWorkbook.Worksheets.Add(sheetName)
        End If
        targetWorkbook.Worksheets(sheetName).CopyFrom(sheet, options)
    Next sheet
End Sub