Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy or Move Excel Worksheets using C++
C++ code sample for copying and moving Excel worksheets
// 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")));
// Source directory path.
StringPtr srcDir = new String("D:\\Work\\Aspose\\01_SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("D:\\Work\\Aspose\\02_OutputDirectory\\");
// Load the input Excel files
intrusive_ptr<IWorkbook> sourceWorkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));
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 to copy
intrusive_ptr<IWorksheet> worksheet = sheets->GetObjectByIndex(0);
// Add a new woksheet
destinationWorkbook->GetIWorksheets()->AddIWorksheet(new String("Test Sheet"));
// Copy the previously retrieved worksheet to the new workbook
destinationWorkbook->GetIWorksheets()->GetObjectByIndex(new String("Test Sheet"))->Copy(worksheet);
// Save the Excel file
destinationWorkbook->Save(outDir->StringAppend(new String("book1_out.xlsx")));
// 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")));
// 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")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment