Como criar um gráfico de pizza no arquivo do Excel usando C++

Neste exemplo, entenderemos como criar gráfico de pizza no arquivo Excel usando C++. Você pode inserir o gráfico de pizza na planilha do Excel no Microsoft Windows, Linux, etc. programaticamente em C++.

Etapas para criar um gráfico de pizza no arquivo do Excel usando C++

  1. Configure o pacote Aspose.Cells.Cpp com o Gerenciador de pacotes NuGet
  2. Adicionar referência ao namespace Aspose::Cells
  3. Inicialize o objeto de classe Workbook para criar um novo arquivo do Excel
  4. Adicionar valores de amostra às células
  5. Adicionar coleção de séries e definir dados de categoria
  6. Formate os datalabels usando a interface IDataLabels
  7. Salve o arquivo Excel de saída após inserir um gráfico de pizza em C++

No exemplo abaixo, você aprenderá como inserir gráfico de pizza no Excel com C++. Inicialize uma pasta de trabalho em branco e faça um gráfico de pizza no arquivo Excel usando C++ simplesmente usando algumas chamadas de API.

Código para inserir gráfico de pizza no Excel com C++

#pragma once
#include "Aspose.Cells.h"
class ExcelPieChart
{
void CreateExcelPieChart()
{
// Set the license for Aspose.Cells API for creating pie chart
intrusive_ptr<License> CellChartLicense = new License();
CellChartLicense->SetLicense(new String("Aspose.Total.lic"));
// Instantiate the Workbook object to load Excel file for inserting pie chart
intrusive_ptr<IWorkbook> ChartWorkbook = Factory::CreateIWorkbook();
//Accessing a worksheet using its index
intrusive_ptr<IWorksheet> ChartWorksheet = ChartWorkbook->GetIWorksheets()->GetObjectByIndex(0);
// Adding sample values to cells
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue("Quarters");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue("1st_Qtr");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue("2nd_Qtr");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue("3rd_Qtr");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("A5"))->PutValue("4th_Qtr");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B1"))->PutValue("Sales");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue("6.3");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue("3.1");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B4"))->PutValue("2.2");
ChartWorksheet->GetICells()->GetObjectByIndex(new String("B5"))->PutValue("1.9");
// Adding a Pie chart to the worksheet in the workbook
int chartIndex = ChartWorksheet->GetICharts()->Add(ChartType::ChartType_Pie, 10, 2, 34, 13);
// Accessing the instance of the newly created Pie chart
intrusive_ptr<Aspose::Cells::Charts::IChart> PieChart = ChartWorksheet->GetICharts()->GetObjectByIndex(chartIndex);
// Adding SeriesCollection to the chart from "A2" to "B5"
PieChart->GetNISeries()->Add(new String("A2:B5"), true);
PieChart->GetNISeries()->SetCategoryData(new String("A2:A5"));
PieChart->GetITitle()->SetText(new String("Sales By Quarter"));
PieChart->GetITitle()->GetIFont()->SetColor(Systems::Drawing::Color::GetBlue());
PieChart->GetITitle()->GetIFont()->SetBold(true);
PieChart->GetITitle()->GetIFont()->SetSize(11);
// Format the datalebels of all series
for (int i = 0; i < PieChart->GetNISeries()->GetCount(); i++)
{
intrusive_ptr<IDataLabels> dataLabels = PieChart->GetNISeries()->GetObjectByIndex(i)->GetIDataLabels();
dataLabels->SetShowValue(true);
dataLabels->SetShowPercentage(true);
}
// Save the output Excel file with newly created pie chart
ChartWorkbook->Save(new String ("output.xlsx"));
}
};

Você pode adicionar gráfico de pizza no arquivo do Excel com C++ inserindo os valores do gráfico e formatando os rótulos de dados programaticamente. Você pode criar facilmente o gráfico de pizza sem precisar instalar o MS Excel ou qualquer outra ferramenta. No exemplo anterior, exploramos Como remover a fórmula do arquivo do Excel em C++ que contém o exemplo para remover a fórmula do arquivo do Excel.

 Português