In this tutorial, we will learn how to create Excel file using C++. You can create an XLSX or XLS file using C++ on MS Windows or Linux programmatically in C++.
Steps to Create Excel File using C++
- Configure the Aspose.Cells.Cpp package with NuGet package manager tool
- Add the reference to Aspose::Cells namespace
- Initialize the Workbook Class object to instantiate a blank Excel workbook
- Insert sample values into the cells of Excel file
- Save the output Excel file after inserting data using C++
In the following example, you will explore how to make Excel file using C++. Initialize an empty workbook and insert sample values into the cells simply by following few steps.
Code to Create Excel File in C++
#pragma once | |
#include "Aspose.Cells.h" | |
class ExcelWorkbook | |
{ | |
void CreateExcelWorkbook() | |
{ | |
// Set the license for Aspose.Cells API for creating workbook | |
intrusive_ptr<License> CellCreateLicense = new License(); | |
CellCreateLicense->SetLicense(new String("Aspose.Total.lic")); | |
// Instantiate the Workbook object to create an empty XLSX file | |
intrusive_ptr<IWorkbook> CreateWorkbook = Factory::CreateIWorkbook(); | |
//Accessing a worksheet using its index for inserting data | |
intrusive_ptr<IWorksheet> CreateWorksheet = CreateWorkbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Adding sample data and values to cells for filtering | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue("Customers Report"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue("C_ID"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue("C_Name"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue("C001"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue("Customer1"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue("C002"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B4"))->PutValue("Customer2"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A5"))->PutValue("C003"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B5"))->PutValue("Customer3"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("A6"))->PutValue("C004"); | |
CreateWorksheet->GetICells()->GetObjectByIndex(new String("B6"))->PutValue("Customer4"); | |
// Save the output Excel file with inserted data | |
CreateWorkbook->Save(new String ("WorkbookOutput.xlsx")); | |
} | |
}; | |
You can generate Excel file using C++ by creating empty workbook and inserting sample data and values into the cells. You do not need to install MS Excel or any other application to generate the excel file with this code snippet. In previous example, we learned How to Add Filter in XLSX File using C++ that explains adding a filter in XLSX file.