Back to Devexpress

Worksheet.CopyFrom(Worksheet, WorksheetCopyOptions) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-copyfrom-x28-devexpress-dot-spreadsheet-dot-worksheet-devexpress-dot-spreadsheet-dot-worksheetcopyoptions-x29.md

latest3.3 KB
Original Source

Worksheet.CopyFrom(Worksheet, WorksheetCopyOptions) Method

Copies data from the specified worksheet to the current worksheet and allows you to specify copy options.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void CopyFrom(
    Worksheet source,
    WorksheetCopyOptions options
)
vb
Sub CopyFrom(
    source As Worksheet,
    options As WorksheetCopyOptions
)

Parameters

NameTypeDescription
sourceWorksheet

The source worksheet.

| | options | WorksheetCopyOptions |

An object that defines copy options.

|

Example

The following example copies all data from one worksheet to another except for cell comments:

csharp
using DevExpress.Spreadsheet;
// ...

using (Workbook sourceWorkbook = new Workbook())
using (Workbook targetWorkbook = new Workbook())
{
    targetWorkbook.LoadDocument(@"Documents\Book1.xlsx");
    sourceWorkbook.LoadDocument(@"Documents\Book2.xlsx");
    // Add a new worksheet to the destination workbook.
    targetWorkbook.Worksheets.Add("Sheet1_Copy");
    // Specify copy options.
    var copyOptions = new WorksheetCopyOptions()
    {
        // Copy all data except for cell comments. 
        PasteOptions = PasteSpecial.All & ~PasteSpecial.Comments
    };
    // Copy data from "Sheet1" in the source workbook
    // to the newly created worksheet in the destination workbook. 
    targetWorkbook.Worksheets["Sheet1_Copy"].CopyFrom(sourceWorkbook.Worksheets["Sheet1"], copyOptions);
    targetWorkbook.SaveDocument("Result.xlsx");
}
vb
Imports DevExpress.Spreadsheet
' ...

Using sourceWorkbook As New Workbook()
Using targetWorkbook As New Workbook()
  targetWorkbook.LoadDocument("Documents\Book1.xlsx")
  sourceWorkbook.LoadDocument("Documents\Book2.xlsx")
  ' Add a new worksheet to the destination workbook.
  targetWorkbook.Worksheets.Add("Sheet1_Copy")
  ' Specify copy options.
  ' Copy all data except for cell comments. 
  Dim copyOptions As New WorksheetCopyOptions() With {
    .PasteOptions = PasteSpecial.All And Not PasteSpecial.Comments
    }
  ' Copy data from "Sheet1" in the source workbook
  ' to the newly created worksheet in the destination workbook. 
  targetWorkbook.Worksheets("Sheet1_Copy").CopyFrom(sourceWorkbook.Worksheets("Sheet1"), copyOptions)
  targetWorkbook.SaveDocument("Result.xlsx")
End Using
End Using

See Also

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace