In certain cases, you need to copy rows and columns in an Excel file without copying the entire worksheet. To perform this operation programmatically, this article covers how to copy rows or columns in an Excel worksheet using C#.
- C# API to Copy Rows and Columns in Excel
- Copy Rows in Excel Worksheet using C#
- Copy Columns in Excel Worksheet using C#
C# API to Copy Rows and Columns in Excel
Aspose.Cells for .NET is a class library that lets you implement Excel automation features within the .NET applications. The API provides simple ways to copy rows and columns within or between the Excel workbooks. You can either download the API or install it via NuGet.
PM> Install-Package Aspose.Cells
Copy Rows in Excel Worksheet using C#
The following are the steps to copy a row in an Excel worksheet using C#.
- First, load the Excel file using Workbook class.
- Get the desired Worksheet from Workbook.Worksheets collection.
- Copy desired row by specifying the source and target row index in Worksheet.Cells.CopyRow(Workheet.Cells, sourceRowIndex, targetRowIndex) method.
- Finally, save the updated Excel file using Workbook.Save(string) method.
The following code sample shows how to copy a row in Excel worksheet in C#.
Copy Columns in Excel Worksheet using C#
The following are the steps to copy a column in an Excel worksheet using C#.
- First, load the Excel file using Workbook class.
- Get the desired Worksheet from Workbook.Worksheets collection.
- Copy desired column by specifying the source and target column index in Worksheet.Cells.CopyColumn(Workheet.Cells, sourceColumnIndex, targetColumnIndex) method.
- Finally, save the updated Excel file using Workbook.Save(string) method.
The following code sample shows how to copy a column in an Excel worksheet using C#.
Get a Free License
You can use Aspose.Cells for .NET without evaluation limitations using a temporary license.
Conclusion
In this article, you have learned how to copy rows and columns in Excel worksheets using C#. You can use the provided code samples to copy rows or columns within different Excel workbooks as well. In addition, you can explore the documentation of Aspose.Cells for .NET. In case you would have any queries, feel free to post to our forum.