Copying rows and columns is a common task that is performed while working with Excel files. There might be situations where you need to copy rows or columns in Excel files programmatically. For such cases, this article will teach you how to copy rows and columns in Excel files using C++.
- C++ API for Copying Rows and Columns in Excel Files
- Copy a Single Row in an Excel File using C++
- Copying Multiple Rows in an Excel File using C++
- Copy a Single Column in an Excel File using C++
- Copying Multiple Columns in an Excel File using C++
C++ API for Copying Rows and Columns in Excel Files
Aspose.Cells for C++ is a native C++ library that allows you to create, read and modify Excel files without requiring Microsoft Excel to be installed. The API also provides the ability to copy rows and columns in Excel files. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Cells.Cpp
Copy a Single Row in an Excel File using C++
The following is the image of the source Excel file that we will use in the following examples.
The following are the steps to copy a single row in an Excel file using C++.
- Load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to copy the row.
- Copy the row using the CopyIRow(intrusive_ptr<Aspose::Cells::ICells> sourceCells, Aspose::Cells::Systems::Int32 sourceRowIndex, Aspose::Cells::Systems::Int32 destinationRowIndex) method.
- Save the Excel file using the IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to copy a row in an Excel file using C++.
Copying Multiple Rows in an Excel File using C++
In order to copy multiple rows, we will use the CopyIRows method that accepts an additional parameter indicating the total number of rows to be copied. To copy multiple rows, follow the steps given below.
- Load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to copy the rows.
- Copy the rows using the CopyIRows(intrusive_ptr<Aspose::Cells::ICells> sourceCells, Aspose::Cells::Systems::Int32 sourceRowIndex, Aspose::Cells::Systems::Int32 destinationRowIndex, Aspose::Cells::Systems::Int32 rowNumber) method.
- Save the Excel file using the IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to copy multiple rows in an Excel file using C++.
Copy a Single Column in an Excel File using C++
The following are the steps to copy a single column in an Excel file using C++.
- Load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to copy the column.
- Copy the column using the CopyIColumn(intrusive_ptr<Aspose::Cells::ICells> sourceCells, Aspose::Cells::Systems::Int32 sourceColumnIndex, Aspose::Cells::Systems::Int32 destinationColumnIndex) method.
- Save the Excel file using the IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to copy a single column in an Excel file using C++.
Copying Multiple Columns in an Excel File using C++
In order to copy multiple columns, we will use the CopyIColumns method that accepts an additional parameter indicating the total number of columns to be copied. To copy multiple columns, follow the steps given below.
- Load the Excel file using the IWorkbook class.
- Retrieve the worksheet where you want to copy the columns.
- Copy the columns using the CopyIColumns(intrusive_ptr<Aspose::Cells::ICells> sourceCells, Aspose::Cells::Systems::Int32 sourceColumnIndex, Aspose::Cells::Systems::Int32 destinationColumnIndex, Aspose::Cells::Systems::Int32 columnNumber) method.
- Save the Excel file using the IWorkbook->Save(intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following sample code shows how to copy multiple columns in an Excel file using C++.
Get a Free License
In order to try the API without evaluation limitations, you can request a free temporary license.
Conclusion
In this article, you have learned how to copy rows and columns in an Excel file using C++. The shared code samples show how to copy single and multiple rows and columns in an Excel file. We used the Aspose.Cells for C++ API to achieve this. It is a robust API that provides many additional features for working with Excel files. You can explore the API in detail by visiting the official documentation. In case of any queries, please feel free to reach us at our free support forum.