[]
        
(Showing Draft Content)

Copy and Paste

In Spread for WPF, you can easily copy and paste data within workbooks and worksheets. You can select a cell or a range of cells from one region (source range) in a worksheet and paste it into another region (destination range) within the same or a different worksheet of the same or different workbook.

Copy Cell Ranges

Spread for WPF provides a powerful and flexible Copy feature, allowing users to copy data, formatting, formulas, and other cell contents from one or more cell ranges to a specified target range or to the system clipboard.

By using the Copy() method of the IRange interface, users can select from multiple overloads to meet diverse copy requirements in various data processing scenarios.

Copying Data to a Specified Cell Location

You can copy data and styles from a source cell range to a target location, starting at a specified row and column index. The optional ManipulationOptions parameter allows you to control the type of content being copied (such as values only, formatting, formulas, or all content).

C#

// Set data.
spreadSheet1.Workbook.ActiveSheet.Cells["A1"].Value = 10;
spreadSheet1.Workbook.ActiveSheet.Cells["A2"].Value = 20;
spreadSheet1.Workbook.ActiveSheet.Cells["A1:A2"].Font.Color = GrapeCity.Spreadsheet.Color.FromArgb(255, 0, 0);
spreadSheet1.Workbook.ActiveSheet.Cells["A1:A2"].Font.Italic = true;
spreadSheet1.Workbook.ActiveSheet.Cells["B1"].Value = 1;
spreadSheet1.Workbook.ActiveSheet.Cells["B2"].Value = 2;

// Copy values only to C3:D4.
spreadSheet1.Workbook.ActiveSheet.Cells["A1:B2"].Copy(3, 3, GrapeCity.Spreadsheet.ManipulationOptions.Values);

// Copy all content (including values, formatting, formulas, etc.) to C3:D4.
spreadSheet1.Workbook.ActiveSheet.Cells["A1:B2"].Copy(3, 3, GrapeCity.Spreadsheet.ManipulationOptions.All);

VB

' Set data.
spreadSheet1.Workbook.ActiveSheet.Cells("A1").Value = 10
spreadSheet1.Workbook.ActiveSheet.Cells("A2").Value = 20
spreadSheet1.Workbook.ActiveSheet.Cells("A1:A2").Font.Color = GrapeCity.Spreadsheet.Color.FromArgb(255, 0, 0)
spreadSheet1.Workbook.ActiveSheet.Cells("A1:A2").Font.Italic = True
spreadSheet1.Workbook.ActiveSheet.Cells("B1").Value = 1
spreadSheet1.Workbook.ActiveSheet.Cells("B2").Value = 2

' Copy values only to C3:D4.
spreadSheet1.Workbook.ActiveSheet.Cells("A1:B2").Copy(3, 3, GrapeCity.Spreadsheet.ManipulationOptions.Values)

' Copy all content (including values, formatting, formulas, etc.) to C3:D4.
spreadSheet1.Workbook.ActiveSheet.Cells("A1:B2").Copy(3, 3, GrapeCity.Spreadsheet.ManipulationOptions.All)

Copying Data to an Existing Cell Range

You can copy the contents of a source range to a predefined target cell range, which is useful for scenarios where precise control over the target area is required.

C#

IRange currentRange = spreadSheet1.Workbook.ActiveSheet.Cells["A1:B2"];
IRange targetRange = spreadSheet1.Workbook.ActiveSheet.Cells["D1:E2"];
currentRange.Copy(targetRange);

VB

Dim currentRange As IRange = spreadSheet1.Workbook.ActiveSheet.Cells("A1:B2")
Dim targetRange As IRange = spreadSheet1.Workbook.ActiveSheet.Cells("D1:E2")
currentRange.Copy(targetRange)

Copying Data to a Target Range Specified by String

Specify the target range using a string in standard cell reference format (e.g., "A1:B2") to perform quick copy operations.

C#

spreadSheet1.Workbook.ActiveSheet.Cells["A1:B2"].Copy("E1:F2");

VB

spreadSheet1.Workbook.ActiveSheet.Cells("A1:B2").Copy("E1:F2")

Copying to the System Clipboard

Copy the selected cell range to the system clipboard for cross-application pasting. Optional parameters allow you to control whether to display the copy UI and whether to exclude hidden ranges.

C#

// Copy data to the clipboard without showing the UI and excluding hidden ranges.
spreadSheet1.Workbook.ActiveSheet.Cells["A1:B2"].Copy(showUI: false, excludeHidden: true);

// Show the copy UI while copying to the clipboard.
spreadSheet1.Workbook.ActiveSheet.Cells["A1:B2"].Copy(showUI: true);

VB

' Copy data to the clipboard without showing the UI and excluding hidden ranges.
spreadSheet1.Workbook.ActiveSheet.Cells("A1:B2").Copy(showUI:=False, excludeHidden:=True)

' Show the copy UI while copying to the clipboard.
spreadSheet1.Workbook.ActiveSheet.Cells("A1:B2").Copy(showUI:=True)

Copy and Paste between Worksheets

To copy data from one cell range to another within the same worksheet or different worksheets in the same workbook, you can use the Copy method of the IRange interface. This method has four overloads, allowing flexibility in how data is copied.

The following example code uses one of the overloads of the Copy method that accepts the ManipulationOptions enumeration as a parameter.

C#

// Copy and paste a range in the same worksheet.
spreadSheet1.Workbook.ActiveSheet.Cells[0, 0, 2, 2].Copy(3, 3, GrapeCity.Spreadsheet.ManipulationOptions.Values);

VB

' Copy and paste a range in the same worksheet.
spreadSheet1.Workbook.ActiveSheet.Cells(0, 0, 2, 2).Copy(3, 3, GrapeCity.Spreadsheet.ManipulationOptions.Values)

Copy and Paste between Workbooks

To copy data from one workbook to another within the same workbook set, you can also use the Copy method of the IRange interface. This method allows you to select a range of cells from the source workbook and specify the destination cell in the other workbook where the copied data will be pasted.

The following example code copies the range A1:C3 from Book1 and pastes it to the cell D4 in Book2. Also, the ManipulationOptions.All enumeration ensures that all features of the copied cells are transferred.

C#

// Copy and paste a range to another workbook.
spreadSheet1.Workbook.Name = "Book1";
spreadSheet2.Workbook.Name = "Book2";
spreadSheet1.Workbook.ActiveSheet.Cells["A1:C3"].Copy(spreadSheet2.Workbook.ActiveSheet.Cells["D4"], GrapeCity.Spreadsheet.ManipulationOptions.All);

VB

' Copy and paste a range to another workbook.
spreadSheet1.Workbook.Name = "Book1"
spreadSheet2.Workbook.Name = "Book2"
spreadSheet1.Workbook.ActiveSheet.Cells("A1:C3").Copy(spreadSheet2.Workbook.ActiveSheet.Cells("D4"), GrapeCity.Spreadsheet.ManipulationOptions.All)