Crie gráficos Microsoft® Excel e converta em imagens via C++

Converta gráficos de documentos do Excel em imagens e também crie gráficos, incluindo gráficos de pizza, pirâmide, linhas e bolhas em aplicativos baseados em C++.

 

Usando gráficos do Excel, é possível ter uma visão geral e analisar os dados facilmente para tomar decisões corretas. C++ Biblioteca Excel suporta a criação de diferentes gráficos listados por enum Aspose::Cells::Gráficos::ChartType incluindo gráficos de área, barra, pizza, pirâmide, linha e bolha. Além disso, para conversão de gráficos em imagens, API fornece um Para imagem mehtod no formato de imagem necessário.

Crie gráficos do Excel

O processo de criação de gráfico do Excel é criar uma instância do Aula de apostila e selecione o desejado Planilha . Adicione o gráfico usando Adicionar método com parâmetros relevantes, incluindo tipo de gráfico. Acesse o gráfico via índice e Adicionar a fonte de dados do gráfico.

Código C++ para criar gráficos 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();

Converter gráficos em imagens

Para converter gráficos, o processo é primeiro criar um gráfico do tipo relevante usando o código acima ou acessá-lo na planilha relevante. Defina o caminho de salvamento de saída para a imagem e use o método ToImage para conversão.

Código C++ para converter gráficos do 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();