Copy or Move Excel Worksheets

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

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.

The following is the sample code to copy a worksheet within an Excel workbook using C++.

// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Load the Excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));
// Retrieve worksheets
intrusive_ptr<IWorksheetCollection> sheets = workbook->GetIWorksheets();
// Copy data to a new sheet from an existing sheet within the workbook.
sheets->AddCopy(new String("Sheet1"));
// Save the Excel file
workbook->Save(outDir->StringAppend(new String("Sample1_out.xlsx")));

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.

The following is the sample code to copy a worksheet from one Excel workbook to another using C++.

// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Load the source Excel file
intrusive_ptr<IWorkbook> sourceWorkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));
// Load the destination Excel file
intrusive_ptr<IWorkbook> destinationWorkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("book1.xlsx")));
// Retrieve source workbook's worksheets
intrusive_ptr<IWorksheetCollection> sheets = sourceWorkbook->GetIWorksheets();
// Retrieve the worksheet that you want to copy
intrusive_ptr<IWorksheet> worksheet = sheets->GetObjectByIndex(0);
// Copy the previously retrieved worksheet to the new workbook
destinationWorkbook->GetIWorksheets()->GetObjectByIndex(0)->Copy(worksheet);
// Save the Excel file
destinationWorkbook->Save(outDir->StringAppend(new String("book1_out.xlsx")));

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.

The following is the sample code to move the worksheet to a different position within the same workbook using C++.

// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Load the Excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));
// Retrieve worksheets
intrusive_ptr<IWorksheetCollection> sheets = workbook->GetIWorksheets();
// Access the first sheet
intrusive_ptr<IWorksheet> sheet = sheets->GetObjectByIndex(0);
// Move the first sheet to the second position in the workbook.
sheet->MoveTo(1);
// Save the Excel file
workbook->Save(outDir->StringAppend(new String("Sample1_out.xlsx")));

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.

See Also