How to Copy Cell Content in Excel in C#

In this tutorial, we delve into the process of copying Excel cell content using IronXL in C#. The video guides viewers through copying single cells, entire rows, columns, and even two-dimensional ranges programmatically. To begin, ensure the IronXL library is installed via the NuGet package manager. The demonstration involves importing the IronXL namespace, which provides access to necessary classes and methods. By using functions like GetColumn and GetRow, viewers learn to copy data from specified cells or ranges and paste it in desired locations within the same worksheet. The tutorial also covers saving the workbook using the SaveAs method, allowing users to verify the results. Key examples include copying cell data from C10 to B13, replicating column A into column H, and duplicating a range from D6 to F8. Finally, viewers are encouraged to explore the software's capabilities by downloading it from the provided link. This comprehensive guide highlights the versatility and power of IronXL in automating Excel file manipulation.

using IronXL;
using System;

class ExcelManipulation
{
    static void Main()
    {
        // Load the existing Excel file
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Select the first worksheet
        WorkSheet sheet = workbook.WorkSheets.First();

        // Example 1: Copy cell data from C10 to B13
        sheet["B13"].Value = sheet["C10"].Value;

        // Example 2: Copy column A to column H
        for (int row = 0; row < sheet.RowCount; row++)
        {
            sheet[$"H{row + 1}"].Value = sheet[$"A{row + 1}"].Value;
        }

        // Example 3: Copy range from D6 to F8
        for (int row = 6; row <= 8; row++)
        {
            for (char col = 'D'; col <= 'F'; col++)
            {
                string sourceAddress = $"{col}{row}";
                string destinationAddress = $"{(char)(col + 3)}{row}";
                sheet[destinationAddress].Value = sheet[sourceAddress].Value;
            }
        }

        // Save changes to a new Excel file
        workbook.SaveAs("modified_example.xlsx");

        Console.WriteLine("Excel file manipulation complete. Check 'modified_example.xlsx' for results.");
    }
}
using IronXL;
using System;

class ExcelManipulation
{
    static void Main()
    {
        // Load the existing Excel file
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Select the first worksheet
        WorkSheet sheet = workbook.WorkSheets.First();

        // Example 1: Copy cell data from C10 to B13
        sheet["B13"].Value = sheet["C10"].Value;

        // Example 2: Copy column A to column H
        for (int row = 0; row < sheet.RowCount; row++)
        {
            sheet[$"H{row + 1}"].Value = sheet[$"A{row + 1}"].Value;
        }

        // Example 3: Copy range from D6 to F8
        for (int row = 6; row <= 8; row++)
        {
            for (char col = 'D'; col <= 'F'; col++)
            {
                string sourceAddress = $"{col}{row}";
                string destinationAddress = $"{(char)(col + 3)}{row}";
                sheet[destinationAddress].Value = sheet[sourceAddress].Value;
            }
        }

        // Save changes to a new Excel file
        workbook.SaveAs("modified_example.xlsx");

        Console.WriteLine("Excel file manipulation complete. Check 'modified_example.xlsx' for results.");
    }
}
Imports IronXL
Imports System

Friend Class ExcelManipulation
	Shared Sub Main()
		' Load the existing Excel file
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")

		' Select the first worksheet
		Dim sheet As WorkSheet = workbook.WorkSheets.First()

		' Example 1: Copy cell data from C10 to B13
		sheet("B13").Value = sheet("C10").Value

		' Example 2: Copy column A to column H
		For row As Integer = 0 To sheet.RowCount - 1
			sheet($"H{row + 1}").Value = sheet($"A{row + 1}").Value
		Next row

		' Example 3: Copy range from D6 to F8
		For row As Integer = 6 To 8
			For col As Char = AscW("D"c) To AscW("F"c)
				Dim sourceAddress As String = $"{col}{row}"
				Dim destinationAddress As String = $"{ChrW(AscW(col) + 3)}{row}"
				sheet(destinationAddress).Value = sheet(sourceAddress).Value
			Next col
		Next row

		' Save changes to a new Excel file
		workbook.SaveAs("modified_example.xlsx")

		Console.WriteLine("Excel file manipulation complete. Check 'modified_example.xlsx' for results.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  1. Loading the Excel File: The WorkBook.Load method opens an existing Excel file called "example.xlsx".
  2. Accessing a Worksheet: It then selects the first worksheet from the loaded workbook for manipulation.
  3. Copying Cell Data: The code copies data from cell C10 to B13 directly.
  4. Copying a Column: By iterating through each row, column A's content is duplicated into column H.
  5. Copying a Range: The nested loop iterates over a two-dimensional range D6 to F8 and copies its content to the range G6 to I8.
  6. Saving Changes: Finally, the SaveAs method is used to save the modifications to "modified_example.xlsx".

Further Reading: How to Copy Cells

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Create New Spreadsheets in C#
NEXT >
How to Convert Spreadsheet Files to JSON, CSV, XML & More with IronXL in C#