في هذا الموضوع ، سنشرح خطوات سهلة الاستخدام حول كيفية إنشاء Excel Pie Chart في تطبيق C# .NET. توضح الخطوات أدناه كيفية إنشاء مخطط دائري ديناميكي في تطبيق C# windows برمجيًا.
بشكل عام ، يشير المخطط الدائري إلى البيانات الفئوية بينما يمكن أن تظهر كل شريحة دائرية فئة معينة. في MS Excel ، يمكنك استخدام مجموعة غنية من أدوات الرسم البياني. لذلك ، يمكنك إنشاء مخطط Excel دائري في مشروع C# على الطاير. بعد ذلك ، يمكنك الحفظ بتنسيق Excel XLSX. يمكنك ببساطة فتح ملف XLSX الناتج في بعض عارض Excel لعرض الرسم البياني.
خطوات إنشاء مخطط Excel دائري في C#
- قم بتنزيل حزمة Aspose.Cells for .NET من معرض NuGet
- استيراد مساحات أسماء ذات صلة Aspose.Cells و Aspose.Cells.Charts باستخدام التوجيه
- إنشاء كائن الترخيص وتعيين ملف الترخيص باستخدام طريقة SetLicense
- قم بإنشاء كائن Workbook لإنشاء مصنف Excel جديد
- احصل على ورقة العمل الأولى في المصنف وأضف البيانات إلى ورقة العمل
- أضف مخطط دائري جديد باستخدام عضو تعداد ChartType.Pie وقم بتعيين سلسلة بياناته مع سمات أخرى
- احفظ ملف Excel الناتج الذي يحتوي على المخطط الدائري فيه
في المثال أدناه ، سننشئ مخطط دائري ديناميكيًا استنادًا إلى بيانات المبيعات ربع السنوية. سيتم أيضًا لصق بيانات المصدر للمخطط عبر التعليمات البرمجية. علاوة على ذلك ، قمنا بتعيين عنوان المخطط وتحديد خصائص تسميات البيانات لإظهار القيم والنسب المئوية على الشرائح الدائرية. يمكننا استخدام تعداد ChartType لتحديد أو تعيين أنواع أخرى مماثلة لرسم المخططات.
نموذج تعليمة برمجية لإنشاء مخطط Excel دائري في C#
using System; | |
using System.Drawing; | |
using System.Windows.Forms; | |
//Add reference to following namespaces for Aspose.Cells for .NET API | |
using Aspose.Cells; | |
using Aspose.Cells.Charts; | |
namespace CreateExcelPieChart | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
//Important: Initialize License object and set it first before using any other APIs | |
//Aspose.Cells for .NET | |
Aspose.Cells.License AsposeCellslic = new Aspose.Cells.License(); | |
AsposeCellslic.SetLicense("Aspose.Cells.lic"); | |
//Create a new Excel Workbook | |
Workbook ExcelChartWorkbook = new Workbook(); | |
//Obtain first worksheet (i.e., default sheet) | |
Worksheet ExcelChartWorksheet = ExcelChartWorkbook.Worksheets[0]; | |
//Set the name of the sheet | |
ExcelChartWorksheet.Name = "PieChart"; | |
//Get worksheet cells collection from the sheet | |
Cells WorksheetCells = ExcelChartWorksheet.Cells; | |
//Input values into the cells, i.e., the source data for the pie chart | |
WorksheetCells["A1"].PutValue("Quarters"); | |
WorksheetCells["A2"].PutValue("1st_Qtr"); | |
WorksheetCells["A3"].PutValue("2nd_Qtr"); | |
WorksheetCells["A4"].PutValue("3rd_Qtr"); | |
WorksheetCells["A5"].PutValue("4th_Qtr"); | |
WorksheetCells["B1"].PutValue("Sales"); | |
WorksheetCells["B2"].PutValue(6.3); | |
WorksheetCells["B3"].PutValue(3.1); | |
WorksheetCells["B4"].PutValue(2.2); | |
WorksheetCells["B5"].PutValue(1.9); | |
//Create or add excel pie chart | |
int chart_Index = 0; | |
chart_Index = ExcelChartWorksheet.Charts.Add(ChartType.Pie, 10, 2, 34, 13); | |
Chart WorksheetChart = ExcelChartWorksheet.Charts[chart_Index]; | |
//Set the data series and category data for the pie chart | |
WorksheetChart.NSeries.Add("B2:B5", true); | |
WorksheetChart.NSeries.CategoryData = "A2:A5"; | |
//Set chart title properties | |
WorksheetChart.Title.Text = "Sales By Quarter"; | |
WorksheetChart.Title.Font.Color = Color.Blue; | |
WorksheetChart.Title.Font.IsBold = true; | |
WorksheetChart.Title.Font.Size = 11; | |
//Set the data labels attributes of the pie chart slices | |
DataLabels data_labels; | |
for (int i = 0; i < WorksheetChart.NSeries.Count; i++) | |
{ | |
data_labels = WorksheetChart.NSeries[i].DataLabels; | |
data_labels.ShowValue = true; | |
data_labels.ShowPercentage = true; | |
} | |
//Save the excel file containing the chart | |
ExcelChartWorkbook.Save("e:\\test2\\pie_chart.xlsx"); | |
} | |
} | |
} |
يقوم مقتطف الشفرة أعلاه بإدراج بعض البيانات في خلايا ورقة العمل الأولى ثم يقوم بإنشاء رسم بياني Excel دائري في C# بناءً على مصدر البيانات هذا. أخيرًا ، يحفظ ملف Excel XLSX الذي سيحتوي على مخطط دائري. علاوة على ذلك ، يمكنك قم بتحويل مخطط Excel إلى JPG في C# بدون Interop أيضًا.