Excel files are used for managing complex data such as departmental budgets or yearly sales reports. There might be situations where you have a ready-made template that you want to use for creating a report. For this, you can create a copy of the template worksheet. You might also need to organize the worksheets to demonstrate the flow of data. For such cases, you can rearrange the worksheets according to your requirements. In light of this, you will learn how to copy and move worksheets programmatically using C++.
- C++ API to Copy or Move Excel Worksheets
- Copy Worksheets within an Excel Workbook using C++
- Copy Worksheets from One Workbook to Another
- Move the Worksheet to a Different Position using C++
- Get a Free License
C++ API to Copy or Move Excel Worksheets
Aspose.Cells for C++ is a native C++ library that allows you to create, read and update Excel files without requiring Microsoft Excel to be installed. The API also supports copying and moving Excel worksheets. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Cells.Cpp
Copy Worksheets within an Excel Workbook using C++
The following are the steps to copy worksheets within an Excel workbook.
- Load the Excel file using the IWorkbook class.
- Retrieve the worksheet collection using the IWorkbook->GetIWorksheets() method.
- Add the copy of the required worksheet using IWorksheetCollection->AddCopy (intrusive_ptr<Aspose::Cells::Systems::String> sheetName) method.
- Finally, save the Excel file using IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following is the sample code to copy a worksheet within an Excel workbook using C++.
Copy Worksheets from One Workbook to Another
Aspose.Cells for C++ also allows you to copy worksheets between two Excel files. The following are the steps to copy a worksheet from one Excel file to another.
- Firstly, load the source and destination Excel files using the IWorkbook class.
- Retrieve the worksheet collection of the source workbook using the IWorkbook->GetIWorksheets() method.
- Retrieve the worksheet you want to copy using IWorksheetCollection->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Copy the worksheet to the destination workbook using the IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index)->Copy (intrusive_ptr<Aspose::Cells::IWorksheet> sourceSheet) method.
- Finally, save the destination Excel file using IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following is the sample code to copy a worksheet from one Excel workbook to another using C++.
Move the Worksheet to a Different Position using C++
The following are the steps to change the position of the worksheet within an Excel Workbook.
- Load the Excel file using the IWorkbook class.
- Retrieve the worksheet collection using the IWorkbook->GetIWorksheets() method.
- Retrieve the worksheet you want to move using IWorksheetCollection->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Move the worksheet to the desired location using IWorksheet->MoveTo (Aspose::Cells::Systems::Int32 index) method.
- Finally, save the Excel file using IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName) method.
The following is the sample code to move the worksheet to a different position within the same workbook using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to copy worksheets within the same Excel workbook or between different workbooks. Furthermore, you have seen how to change the position of the worksheet within an Excel file. Aspose.Cells for C++ provides many more features for working with Excel files that you can explore by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.