Additionally, the Copy method also retains styling, enabling efficient and accurate data replication within a single or multiple worksheets using IronXL.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Copy a Single Cell Example

To copy the content of a selected cell, you can use the Copy method. Pass the worksheet object as the first parameter and the starting position as the second parameter. The Copy method also retains any styling that the cell contains.

:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-single-cell.cs
using IronXL;

// Load the Excel workbook from the file "sample.xlsx"
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Access the worksheet named "Sheet1"
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Copy the content from cell A1 to cell B3 within the same worksheet
workSheet["A1"].Copy(workSheet["B3"]);

// Save the modified workbook to a new file named "copySingleCell.xlsx"
workBook.SaveAs("copySingleCell.xlsx");
Imports IronXL



' Load the Excel workbook from the file "sample.xlsx"

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")



' Access the worksheet named "Sheet1"

Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")



' Copy the content from cell A1 to cell B3 within the same worksheet

workSheet("A1").Copy(workSheet("B3"))



' Save the modified workbook to a new file named "copySingleCell.xlsx"

workBook.SaveAs("copySingleCell.xlsx")
$vbLabelText   $csharpLabel

Output Spreadsheet

Copy Single Cell

Copy Cell Range Example

Similar to the Clear method, this method is also available in the Range class, allowing you to execute it on any range, regardless of its size. Here are some examples:

  • Copy a single cell (C10):

    workSheet["C10"].Copy(workBook.GetWorkSheet("Sheet1"), "B13");
    workSheet["C10"].Copy(workBook.GetWorkSheet("Sheet1"), "B13");
    workSheet("C10").Copy(workBook.GetWorkSheet("Sheet1"), "B13")
    $vbLabelText   $csharpLabel
  • Copy a column (A):

    workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1");
    workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1");
    workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1")
    $vbLabelText   $csharpLabel
  • Copy a row (4):

    workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15");
    workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15");
    workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15")
    $vbLabelText   $csharpLabel
  • Copy a two-dimensional range (D6:F8):

    workSheet["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17");
    workSheet["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17");
    workSheet("D6:F8").Copy(workBook.GetWorkSheet("Sheet1"), "H17")
    $vbLabelText   $csharpLabel

Please note
The second parameter accepts an address location that marks the starting point of data entry. The copied data will start from that address and spread rightward and downward.

:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-cell-range.cs
// Import IronXL library to work with Excel files
using IronXL;

// Load an existing workbook from a file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Retrieve the first worksheet from the workbook by name
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Copy a single cell from C10 to B13 in the same worksheet
workSheet["C10"].Copy(workSheet, "B13"); 

// Copy the entire first column (column A) to H1 in the same worksheet
workSheet.GetColumn(0).Copy(workSheet, "H1");

// Copy the entire fourth row (row 4) to A15 in the same worksheet
workSheet.GetRow(3).Copy(workSheet, "A15");

// Copy a two-dimensional range from D6 to F8 to H17 in the same worksheet
workSheet["D6:F8"].Copy(workSheet, "H17");

// Save the changes to a new Excel file
workBook.SaveAs("copyCellRange.xlsx");
' Import IronXL library to work with Excel files

Imports IronXL



' Load an existing workbook from a file

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")



' Retrieve the first worksheet from the workbook by name

Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")



' Copy a single cell from C10 to B13 in the same worksheet

workSheet("C10").Copy(workSheet, "B13")



' Copy the entire first column (column A) to H1 in the same worksheet

workSheet.GetColumn(0).Copy(workSheet, "H1")



' Copy the entire fourth row (row 4) to A15 in the same worksheet

workSheet.GetRow(3).Copy(workSheet, "A15")



' Copy a two-dimensional range from D6 to F8 to H17 in the same worksheet

workSheet("D6:F8").Copy(workSheet, "H17")



' Save the changes to a new Excel file

workBook.SaveAs("copyCellRange.xlsx")
$vbLabelText   $csharpLabel

Output Spreadsheet

Copy Cell Range

Copy Cell Across Worksheet Example

Since the first parameter accepts a worksheet object, it is possible to copy and paste a cell range across different worksheets. Simply pass a different worksheet object as the first parameter.

Please note
In the following example, the first parameter of the Copy method is the "Sheet2" worksheet: workBook.GetWorksheet("Sheet2")

// Example Code: Copy cells across different worksheets in IronXL

using IronXL;

:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-to-other-worksheet.cs
// Example Code: Copy cells across different worksheets in IronXL

using IronXL;

using IronXL;

// Load an existing workbook from a file
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Retrieve the first worksheet from the workbook by its name
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Copy the content of a cell from the first worksheet to another worksheet
// This copies the content from cell A1 in "Sheet1" to cell B3 in "Sheet2"
workSheet["A1"].Copy(workBook.GetWorkSheet("Sheet2"), "B3");

// Save the changes made to a new file
workBook.SaveAs("copyAcrossWorksheet.xlsx");
' Example Code: Copy cells across different worksheets in IronXL



Imports IronXL





' Load an existing workbook from a file

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")



' Retrieve the first worksheet from the workbook by its name

Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")



' Copy the content of a cell from the first worksheet to another worksheet

' This copies the content from cell A1 in "Sheet1" to cell B3 in "Sheet2"

workSheet("A1").Copy(workBook.GetWorkSheet("Sheet2"), "B3")



' Save the changes made to a new file

workBook.SaveAs("copyAcrossWorksheet.xlsx")
$vbLabelText   $csharpLabel

Frequently Asked Questions

How do I copy a single cell using IronXL?

To copy a single cell using IronXL, use the `Copy` method. Pass the worksheet object as the first parameter and the starting position as the second parameter. This method retains any cell styling.

Can I copy a whole column using IronXL?

Yes, you can copy a whole column using IronXL. Use the `Copy` method on the column range and specify the destination worksheet and position.

How do I copy a row in IronXL?

To copy a row in IronXL, use the `GetRow` method to select the row, then use the `Copy` method to specify the destination worksheet and position.

Is it possible to copy a range of cells in IronXL?

Yes, IronXL allows you to copy a range of cells using the `Copy` method. Specify the range and the destination worksheet and starting position.

Can I copy cells across different worksheets in IronXL?

Yes, you can copy cells across different worksheets by passing the destination worksheet object as the first parameter in the `Copy` method.

Does the Copy method in IronXL retain cell formatting?

Yes, the Copy method in IronXL retains the styling and formatting of the cells that are being copied.

What library do I need to install to use IronXL for copying cells?

You need to download and install the IronXL Excel library from NuGet to use its functionalities for copying cells.

What code do I use to copy a cell range from one worksheet to another?

Use the `Copy` method to specify the range and the destination worksheet and starting position. For example: `sourceSheet["D6:F8"].Copy(destinationSheet, "H17");`

How do I specify the destination position when copying cells in IronXL?

The second parameter of the `Copy` method specifies the starting address in the destination worksheet where the copied data will begin.

Is it possible to automate the process of copying cells in C# using IronXL?

Yes, IronXL allows you to automate the process of copying cells in C# by using methods like `Copy` along with other IronXL functionalities.

Chaknith related to Copy Cell Across Worksheet Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.