Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/f46aa511e63213a2f2b888ab9b51c44c to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f46aa511e63213a2f2b888ab9b51c44c to your computer and use it in GitHub Desktop.
Convert Excel to CSV and CSV to Excel format using C++
Convert Excel to CSV and CSV to Excel format using C++
// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Create CSV LoadOptions object
intrusive_ptr<ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat_CSV);
// Load the input Excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.csv")), loadOptions);
// Save output CSV file
workbook->Save(outDir->StringAppend(new String("Sample1_out.xlsx")), SaveFormat_Xlsx);
// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Load the input Excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.xlsx")));
// Save output CSV file
workbook->Save(outDir->StringAppend(new String("Sample1_out.csv")), SaveFormat_CSV);
// 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 the worksheets of the workbook.
intrusive_ptr<IWorksheetCollection> sheets = workbook->GetIWorksheets();
// Create string builder object for string concatenations.
intrusive_ptr<Aspose::Cells::Systems::Text::StringBuilder> stringBuilder = new Aspose::Cells::Systems::Text::StringBuilder();
for (int sheet = 0; sheet < sheets->GetCount(); sheet++)
{
// Retrieve the worksheet to copy
intrusive_ptr<IWorksheet> worksheet = sheets->GetObjectByIndex(sheet);
// Create a instance of the IWorkbook class to represent the new workbook
intrusive_ptr<IWorkbook> newWorkbook = Factory::CreateIWorkbook();
// Copy the previously retrieved worksheet to the new workbook
newWorkbook->GetIWorksheets()->GetObjectByIndex(0)->Copy(worksheet);
// Clear string builder and create output path with string concatenations.
stringBuilder->Clear();
stringBuilder->Append(outDir);
stringBuilder->Append((StringPtr)new String("Sample1_sheet_"));
stringBuilder->Append(sheet);
stringBuilder->Append((StringPtr)new String("_out.csv"));
// Save output CSV file
newWorkbook->Save(stringBuilder->ToString(), SaveFormat_CSV);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment