Crea grafici Excel Microsoft® e convertili in immagini tramite C++

Converti grafici di documenti Excel in immagini e crea grafici tra cui grafici a torta, a piramide, a linee e a bolle all'interno di applicazioni basate su C++.

 

Utilizzando i grafici Excel, è possibile ottenere un quadro più ampio e analizzare facilmente i dati per prendere le giuste decisioni. C++ Libreria Excel supporta la creazione di diversi grafici elencati per enum Aspose::Cells::Grafici::ChartType inclusi grafici ad area, a barre, a torta, a piramide, a linee e a bolle. Inoltre, per la conversione dei grafici in immagini, lo API mette a disposizione un Immaginare mehtod nel formato immagine richiesto.

Crea grafici Excel

Il processo di creazione del grafico Excel consiste nel creare un’istanza del file Classe cartella di lavoro e selezionare quello desiderato Foglio di lavoro . Aggiungi il grafico utilizzando Aggiungi metodo con parametri rilevanti incluso il tipo di grafico. Accedi al grafico tramite indice e Aggiungere l’origine dati per il grafico.

C++ Codice per Creare Grafici Excel
Aspose::Cells::Startup();
// Path of output XLSM file
U16String outputChartTypeCustom = u"sourceFile.xlsm";
// Create a new workbook
Workbook wkb;
// Get first worksheet which is created by default
Worksheet wks = wkb.GetWorksheets().Get(0);
// Adding sample values to cells
wks.GetCells().Get(u"A1").PutValue(50);
wks.GetCells().Get(u"A2").PutValue(100);
wks.GetCells().Get(u"A3").PutValue(150);
wks.GetCells().Get(u"B1").PutValue(4);
wks.GetCells().Get(u"B2").PutValue(20);
wks.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = wks.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = wks.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the ODS file
wkb.Save(outputChartTypeCustom);
Aspose::Cells::Cleanup();

Converti grafici in immagini

Per convertire i grafici, il processo consiste nel creare innanzitutto un grafico del tipo pertinente utilizzando il codice riportato sopra o accedervi dal foglio pertinente. Definire il percorso di salvataggio dell’output per l’immagine e utilizzare il metodo ToImage per la conversione.

C++ Codice per Convertire Grafici Excel
Aspose::Cells::Startup();
// Output directory path
U16String outDir = u"..\\OutputDirectory\\";
// Path of output image file
U16String outputChartImage = outDir + u"out1image.png";
// Create a new workbook
Workbook wkb;
// Get first worksheet which is created by default
Worksheet wks = wkb.GetWorksheets().Get(0);
// Adding sample values to cells
wks.GetCells().Get(u"A1").PutValue(50);
wks.GetCells().Get(u"A2").PutValue(100);
wks.GetCells().Get(u"A3").PutValue(150);
wks.GetCells().Get(u"B1").PutValue(4);
wks.GetCells().Get(u"B2").PutValue(20);
wks.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = wks.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = wks.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the chart to image file
chart.ToImage(outputChartImage, ImageType::Png);
Aspose::Cells::Cleanup();