Last active
March 25, 2021 14:26
-
-
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++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Convert Excel to CSV and CSV to Excel format using C++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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