How to Copy Cells
The "Copy cell" feature allows you to duplicate the contents of a cell and paste them into one or more other cells. It is a convenient way to replicate data, formulas, formatting, or other attributes within the worksheet.
Additionally, the Copy
method also retains styling, enabling efficient and accurate data replication within a single or multiple worksheets using IronXL.
How to Copy Cell Content
- Download the C# library for copying cells
- Load the existing Excel spreadsheet
- Select the range, row, or column that you want to copy
- Invoke the
Copy
method on the selected range - Pass a destination worksheet and position to the
Copy
method
Get started with IronXL
Start using IronXL in your project today with a free trial.
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;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Copy cell content
workSheet["A1"].Copy(workBook.GetWorkSheet("Sheet1"), "B3");
workBook.SaveAs("copySingleCell.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Copy cell content
workSheet("A1").Copy(workBook.GetWorkSheet("Sheet1"), "B3")
workBook.SaveAs("copySingleCell.xlsx")
Output Spreadsheet
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")
Copy a column (A):
- workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1")
Copy a row (4):
- workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15")
Copy a two-dimensional range (D6:F8):
- workSheet ["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17")
Please note
:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-cell-range.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Copy a single cell(C10)
workSheet["C10"].Copy(workBook.GetWorkSheet("Sheet1"), "B13");
// Copy a column(A)
workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1");
// Copy a row(4)
workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15");
// Copy a two-dimensional range(D6:F8)
workSheet["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17");
workBook.SaveAs("copyCellRange.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Copy a single cell(C10)
workSheet("C10").Copy(workBook.GetWorkSheet("Sheet1"), "B13")
' Copy a column(A)
workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1")
' Copy a row(4)
workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15")
' Copy a two-dimensional range(D6:F8)
workSheet("D6:F8").Copy(workBook.GetWorkSheet("Sheet1"), "H17")
workBook.SaveAs("copyCellRange.xlsx")
Output Spreadsheet
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
:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-to-other-worksheet.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Copy cell content
workSheet["A1"].Copy(workBook.GetWorkSheet("Sheet2"), "B3");
workBook.SaveAs("copyAcrossWorksheet.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Copy cell content
workSheet("A1").Copy(workBook.GetWorkSheet("Sheet2"), "B3")
workBook.SaveAs("copyAcrossWorksheet.xlsx")