इस उदाहरण में, हम समझेंगे कि कैसे C++ का उपयोग करके Excel फ़ाइल में पाई चार्ट बनाएं। आप Microsoft Windows, Linux, आदि पर Excel कार्यपत्रक में पाई चार्ट को C++ में प्रोग्रामेटिक रूप से सम्मिलित कर सकते हैं।
C++ का उपयोग करके एक्सेल फाइल में पाई चार्ट बनाने के चरण
- Aspose.Cells.Cpp पैकेज को NuGet पैकेज मैनेजर के साथ कॉन्फ़िगर करें
- Aspose::Cells नाम स्थान का संदर्भ जोड़ें
- नई एक्सेल फ़ाइल बनाने के लिए Workbook क्लास ऑब्जेक्ट को इनिशियलाइज़ करें
- कक्षों में नमूना मान जोड़ें
- श्रृंखला संग्रह जोड़ें और श्रेणी डेटा सेट करें
- IDataLabels इंटरफ़ेस का उपयोग करके datalabels को प्रारूपित करें
- C++ में पाई चार्ट डालने के बाद आउटपुट एक्सेल फाइल को सेव करें
नीचे दिए गए उदाहरण में, आप सीखेंगे कि एक्सेल में C++ के साथ पाई चार्ट कैसे डालें। एक रिक्त कार्यपुस्तिका को प्रारंभ करें और केवल कुछ API कॉलों का उपयोग करके C++* का उपयोग करके एक्सेल फ़ाइल में पाई चार्ट बनाएं।
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")); | |
} | |
}; |
आप चार्ट मानों को सम्मिलित करके और डेटालैबल्स को प्रोग्रामेटिक रूप से स्वरूपित करके C++* के साथ एक्सेल फ़ाइल में पाई चार्ट जोड़ सकते हैं। आप एमएस एक्सेल या किसी अन्य उपकरण को स्थापित किए बिना आसानी से पाई चार्ट बना सकते हैं। पिछले उदाहरण में, हमने C++ में एक्सेल फाइल से फॉर्मूला कैसे निकालें? की खोज की थी जिसमें एक्सेल फ़ाइल से सूत्र निकालने का उदाहरण है।