Hoe Excel-bestand te bewerken in C++

In dit voorbeeld zullen we onderzoeken hoe u bewerkt Excel bestand in C++. U kunt het Excel-bestand programmatisch bewerken in C ++ met behulp van een eenvoudige API-interface die zowel in Microsoft Windows als Linux enz. kan worden gebruikt.

Stappen om Excel-bestand te bewerken in C++

  1. Voeg Aspose.Cells.Cpp toe met het hulpprogramma NuGet-pakketbeheer
  2. Voeg een verwijzing toe aan de Aspose::Cells naamruimte
  3. Maak een exemplaar van Workbook object om het Excel-bestand te laden voor bewerking
  4. Open de cellen A1 en C1 in het werkblad en stel respectievelijk de gegevens en formulereeks in
  5. Bereken de formule voor Workbook
  6. Sla de uitvoerwerkmap met formule op in XLSX in C++

Het volgende voorbeeld wordt gebruikt om heel snel en gemakkelijk toegang te krijgen tot de werkmap en het Excel-bestand in C++* bij te werken met een paar API-aanroepen. Met C ++ kunt u inhoudsgegevens programmatisch instellen op Excel-cellen, zoals Datum, Percentage, Numeriek of een andere waarde.

Code om Excel-bestand te bewerken in C++

#pragma once
#include "Aspose.Cells.h"
class ExcelFileEditing
{
void UpdateCellInExcel()
{
// Add Aspose.Cells for C++ API License
intrusive_ptr<License> CellRenderingLicense = new License();
CellRenderingLicense->SetLicense(new String("CPP.Aspose.Total.lic"));
// Instantiate the Workbook class object to load Excel file for editing
intrusive_ptr<IWorkbook> WbWithDataAndFormula = Factory::CreateIWorkbook();
// Access cell A1 from a first worksheet to set data
intrusive_ptr <ICell> DataCell = WbWithDataAndFormula->GetIWorksheets()->GetObjectByIndex(0)->
GetICells()->GetObjectByIndex(new String("A1"));
// Set some value in cell
DataCell->PutValue(100);
// Access cell C1 from first worksheet to update formula
intrusive_ptr <ICell> ForumulCell = WbWithDataAndFormula->GetIWorksheets()->GetObjectByIndex(0)->
GetICells()->GetObjectByIndex(new String("C1"));
// Update the formula for selected cell
ForumulCell->SetFormula(new String("=Sum(A1,A20)"));
// Calculate the workbook after formula is updated
WbWithDataAndFormula->CalculateFormula();
// Save the output workbook with formula to XLSX
WbWithDataAndFormula->Save(new String("WorkbookWithFormula.xlsx"));
}
};

Dit hele proces van bewerken van Excel-bestanden in C++ wordt bereikt zonder afhankelijkheid van Microsoft Office of Interop. Als u nu het Excel-bestand zelfs als PDF wilt opslaan, kunt u voorbeeld Hoe Excel naar PDF te converteren met C++ verkennen.

 Nederlands