Come creare file Excel usando C++

In questo tutorial impareremo come creare file Excel utilizzando C++. Puoi creare un file XLSX o XLS utilizzando C++ su MS Windows o Linux a livello di codice in C++.

Passaggi per creare file Excel utilizzando C++

  1. Configura il pacchetto Aspose.Cells.Cpp con lo strumento di gestione dei pacchetti NuGet
  2. Aggiungi il riferimento allo spazio dei nomi Aspose::Cells
  3. Inizializza l’oggetto Class Workbook per creare un’istanza di una cartella di lavoro di Excel vuota
  4. Inserisci i valori di esempio nelle celle del file Excel
  5. Salva il file Excel di output dopo aver inserito i dati utilizzando C++

Nell’esempio seguente, esplorerai come creare file Excel utilizzando C++. Inizializza una cartella di lavoro vuota e inserisci i valori di esempio nelle celle seguendo semplicemente alcuni passaggi.

Codice per creare file Excel 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"));
}
};

Puoi generare file Excel utilizzando C++ creando una cartella di lavoro vuota e inserendo dati e valori di esempio nelle celle. Non è necessario installare MS Excel o qualsiasi altra applicazione per generare il file excel con questo frammento di codice. Nell’esempio precedente, abbiamo appreso Come aggiungere un filtro nel file XLSX usando C++ che spiega l’aggiunta di un filtro nel file XLSX.

 Italiano