Excel is a popular format for sharing information. You may find yourself in situations where you have data in an Excel(XLS/XLSX) file that you need to import to a database. For this, you would need to convert the Excel file to CSV format. On the other hand, you may have data in CSV format exported from a database that you need to manipulate further in an Excel file. For such cases, you would need to convert the CSV file to Excel format. In this article, you will learn how to perform both these conversions programmatically using C++.
- C++ API to Convert Excel Files to CSV and CSV files to Excel Format
- Converting Excel files to CSV format using C++
- Convert Excel Worksheets to CSV files
- Converting CSV files to Excel format using C++
- Get a Free License
C++ API to Convert Excel Files to CSV and CSV files to Excel Format
Aspose.Cells for C++ is a native C++ library that allows you to create, read and modify Excel files. Furthermore, the API supports converting Excel files to CSV format and vice-versa. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Cells.Cpp
Converting Excel files to CSV format using C++
Converting Excel files to CSV format is a breeze with Aspose.Cells for C++ API. The following are the steps to convert Excel files to CSV format using C++.
- Load the Excel file using the IWorkbook class.
- Save the file as CSV using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName, Aspose::Cells::SaveFormat saveFormat) method.
The following is the sample code to convert Excel files to CSV format.
Convert Excel Worksheets to CSV files
The previous example converts only the first worksheet if the Excel file contains multiple worksheets. In case you want to convert multiple worksheets to CSV format, you can follow the following steps.
- Load the Excel file using the IWorkbook class.
- Retrieve the worksheets using the IWorkbook->GetIWorksheets() method.
- Loop through the retrieved worksheets.
- Within the loop, you can specify your custom logic to convert specific worksheets to CSV files. In this demonstration, all the worksheets are converted to CSV files.
- Retrieve the worksheet to be converted using IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) method.
- Create an instance of the IWorkbook class to represent a new CSV file.
- Copy the previously retrieved worksheet to the new wokrbook instance using the IWorkbook->GetIWorksheets()->GetObjectByIndex(0)->Copy (intrusive_ptr<Aspose::Cells::IWorksheet> sourceSheet) method.
- Save the new workbook as CSV using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName, Aspose::Cells::SaveFormat saveFormat) method.
The following is the sample code to convert Excel worksheets to CSV files using C++.
Converting CSV files to Excel format using C++
The following are the steps to convert CSV files to Excel format using C++.
- Create an instance of the ILoadOptions class.
- Create an object of the IWorkbook class using the ILoadOptions instance created earlier.
- Save the file in Excel format using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName, Aspose::Cells::SaveFormat saveFormat) method.
The following is the sample code to convert CSV files to Excel format using C++.
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 convert Excel files to CSV format and CSV files to Excel (XLS/XLSX) format using C++. Aspose.Cells for C++ is a vast API that provides many additional features for working with Excel files. You can explore the API in detail by using the official documentation. In case of any questions, please feel free to reach us on our free support forum.