How to Make a Copy of an Excel Sheet in C++

This quick tutorial will educate you on how to make a copy of an Excel sheet in C++. It contains the instructions to configure the environment, detailed steps to accomplish the task, and finally a runnable C++ code along with the code description. Once the data is transferred via Excel sheet copy in C++, the output workbook can be saved as XLSX or XLS file.

Steps to Make a Copy of an Excel Sheet in C++

  1. Add Aspose.Cells.Cpp package from NuGet package manager
  2. Include necessary header files for Aspose.Cells in the program
  3. Load the source Excel file into the Workbook object for copying the worksheet
  4. Get a reference to the first worksheet from the input workbook
  5. Create an empty workbook where the worksheet will be copied
  6. Get a reference to the first worksheet from the new empty workbook
  7. Call copy function of the output worksheet to copy the data from the input worksheet
  8. Save the output workbook on disk after copying worksheet data from the input workbook

Here a sequence of steps is provided to copy sheet from one workbook to another in C++ like the environment is set and then necessary headers files are added. In the next steps, the input workbook is opened and the output workbook is created, references to the first worksheet of each workbook are accessed and finally, the entire data of the source worksheet is copied to the destination worksheet. In the end, the output workbook is saved in XLSX format however you can save it in any other format supported by MS Excel.

Code to Copy Worksheet to Another Workbook in C++

The intrusive_ptr pointer is used to save the pointer to different objects and Factory::CreateIWorkbook function is used to load the existing workbook and create a new workbook as well. You can access the reference to a particular worksheet using the zero-based index with GetIWorksheets() function. Note that data is copied from the source worksheet to the destination worksheet however you can control this copying process by using the CopyOptions class object in the Copy function like you can set a flag to copy values in the destination sheet if a formula is invalid for the destination sheet, set flag to copy names, and set a lot of other properties.

In this simple tutorial, we have learned how to copy a worksheet in Excel using C++ however if you want to learn how to merge cells in Excel using C++ refer to the article on how to merge cells in Excel using C++.

 English