Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Explanation:
WorkBook.Load
method opens an existing Excel file called "example.xlsx".SaveAs
method is used to save the modifications to "modified_example.xlsx".Further Reading: How to Copy Cells