إنشاء مخطط عن طريق معالجة العلامات الذكية
توفر واجهات برمجة التطبيقات Aspose.Cells امتدادالمصمم المصممفئة للعمل باستخدام Smart Markers حيث يتم وضع التنسيق والصيغ في جداول بيانات المصمم ثم معالجتها باستخدامالمصمم المصممفئة لتعبئة البيانات وفقًا للعلامات الذكية المحددة. من الممكن أيضًا إنشاء مخططات Excel من خلال معالجة Smart Markers ، والتي ستتطلب الخطوات التالية.
- إنشاء جدول بيانات المصمم
- معالجة جدول بيانات المصمم مقابل مصدر البيانات المحدد
- إنشاء مخطط على أساس البيانات المأهولة
إنشاء جدول بيانات مصمم
جدول بيانات المصمم هو ملف Excel بسيط تم إنشاؤه باستخدام تطبيق Excel Microsoft أو Aspose.Cells APIs التي تحتوي على التنسيق المرئي والصيغ والعلامات الذكية ، حيث يمكن ملء المحتويات في وقت التشغيل.
من أجل البساطة ، سنقوم بإنشاء جدول بيانات المصمم باستخدام API Aspose.Cells for .NET API ومعالجته لاحقًا مقابل مصدر بيانات تم إنشاؤه ديناميكيًا لأغراض العرض التوضيحي.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook | |
var book = new Workbook(); | |
// Access the first, default Worksheet by passing its index | |
var dataSheet = book.Worksheets[0]; | |
// Name the Worksheet for later reference | |
dataSheet.Name = "ChartData"; | |
// Access the CellsCollection of first Worksheet | |
var cells = dataSheet.Cells; | |
// Insert static data (headers) | |
cells["B1"].PutValue("Item 1"); | |
cells["C1"].PutValue("Item 2"); | |
cells["D1"].PutValue("Item 3"); | |
cells["E1"].PutValue("Item 4"); | |
cells["F1"].PutValue("Item 5"); | |
cells["G1"].PutValue("Item 6"); | |
cells["H1"].PutValue("Item 7"); | |
cells["I1"].PutValue("Item 8"); | |
cells["J1"].PutValue("Item 9"); | |
cells["K1"].PutValue("Item 10"); | |
cells["L1"].PutValue("Item 11"); | |
cells["M1"].PutValue("Item 12"); | |
// Place Smart Markers | |
cells["A2"].PutValue("&=Sales.Year"); | |
cells["B2"].PutValue("&=Sales.Item1"); | |
cells["C2"].PutValue("&=Sales.Item2"); | |
cells["D2"].PutValue("&=Sales.Item3"); | |
cells["E2"].PutValue("&=Sales.Item4"); | |
cells["F2"].PutValue("&=Sales.Item5"); | |
cells["G2"].PutValue("&=Sales.Item6"); | |
cells["H2"].PutValue("&=Sales.Item7"); | |
cells["I2"].PutValue("&=Sales.Item8"); | |
cells["J2"].PutValue("&=Sales.Item9"); | |
cells["K2"].PutValue("&=Sales.Item10"); | |
cells["L2"].PutValue("&=Sales.Item11"); | |
cells["M2"].PutValue("&=Sales.Item12"); |
معالجة جدول بيانات المصمم
من أجل معالجة جدول بيانات المصمم ، يجب أن يكون لدى المرء مصدر بيانات يتوافق مع العلامات الذكية المستخدمة في جدول بيانات المصمم. على سبيل المثال ، قمنا بإنشاء إدخال Smart Marker كـ & = Sales.Year ، والذي يمثل عمود السنة في مبيعات DataTable. في حالة عدم توفر العمود المقابل في مصدر البيانات ، ستتخطى واجهات برمجة التطبيقات Aspose.Cells المعالجة لهذا المحدد الذكي ، ونتيجة لذلك ، لن يتم ملء البيانات الخاصة بـ Smart Marker المحدد.
لتوضيح حالة الاستخدام هذه ، سننشئ مصدر البيانات من البداية ونعالجها مقابل جدول بيانات المصمم الذي تم إنشاؤه في الخطوة السابقة. ومع ذلك ، في سيناريو الوقت الفعلي ، قد تكون البيانات متاحة بالفعل لمزيد من المعالجة حتى تتمكن من تخطي إنشاء مصدر البيانات إذا كانت البيانات متاحة بالفعل.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create an instance of DataTable and name is according to the Smart Markers | |
var table = new DataTable("Sales"); | |
/* | |
* Add columns to the newly created DataTable while specifying the column type | |
* It is important that the DataTable should have at least one column for each | |
* Smart Marker entry from the designer spreadsheet | |
*/ | |
table.Columns.Add("Year", typeof(string)); | |
table.Columns.Add("Item1", typeof(int)); | |
table.Columns.Add("Item2", typeof(int)); | |
table.Columns.Add("Item3", typeof(int)); | |
table.Columns.Add("Item4", typeof(int)); | |
table.Columns.Add("Item5", typeof(int)); | |
table.Columns.Add("Item6", typeof(int)); | |
table.Columns.Add("Item7", typeof(int)); | |
table.Columns.Add("Item8", typeof(int)); | |
table.Columns.Add("Item9", typeof(int)); | |
table.Columns.Add("Item10", typeof(int)); | |
table.Columns.Add("Item11", typeof(int)); | |
table.Columns.Add("Item12", typeof(int)); | |
// Add some rows with data to the DataTable | |
table.Rows.Add("2000", 2310, 0, 110, 15, 20, 25, 30, 1222, 200, 421, 210, 133); | |
table.Rows.Add("2005", 1508, 0, 170, 280, 190, 400, 105, 132, 303, 199, 120, 100); | |
table.Rows.Add("2010", 0, 210, 230, 140, 150, 160, 170, 110, 1999, 1229, 1120, 2300); | |
table.Rows.Add("2015", 3818, 320, 340, 260, 210, 310, 220, 0, 0, 0, 0, 122); |
تعد معالجة Smart Markers بسيطة للغاية كما هو موضح في مقتطف الشفرة التالي.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook | |
var book = new Workbook(); | |
// Access the first, default Worksheet by passing its index | |
var dataSheet = book.Worksheets[0]; | |
// Name the Worksheet for later reference | |
dataSheet.Name = "ChartData"; | |
// Access the CellsCollection of first Worksheet | |
var cells = dataSheet.Cells; | |
// Insert static data (headers) | |
cells["B1"].PutValue("Item 1"); | |
cells["C1"].PutValue("Item 2"); | |
cells["D1"].PutValue("Item 3"); | |
cells["E1"].PutValue("Item 4"); | |
cells["F1"].PutValue("Item 5"); | |
cells["G1"].PutValue("Item 6"); | |
cells["H1"].PutValue("Item 7"); | |
cells["I1"].PutValue("Item 8"); | |
cells["J1"].PutValue("Item 9"); | |
cells["K1"].PutValue("Item 10"); | |
cells["L1"].PutValue("Item 11"); | |
cells["M1"].PutValue("Item 12"); | |
// Place Smart Markers | |
cells["A2"].PutValue("&=Sales.Year"); | |
cells["B2"].PutValue("&=Sales.Item1"); | |
cells["C2"].PutValue("&=Sales.Item2"); | |
cells["D2"].PutValue("&=Sales.Item3"); | |
cells["E2"].PutValue("&=Sales.Item4"); | |
cells["F2"].PutValue("&=Sales.Item5"); | |
cells["G2"].PutValue("&=Sales.Item6"); | |
cells["H2"].PutValue("&=Sales.Item7"); | |
cells["I2"].PutValue("&=Sales.Item8"); | |
cells["J2"].PutValue("&=Sales.Item9"); | |
cells["K2"].PutValue("&=Sales.Item10"); | |
cells["L2"].PutValue("&=Sales.Item11"); | |
cells["M2"].PutValue("&=Sales.Item12"); | |
// Create an instance of DataTable and name is according to the Smart Markers | |
var table = new DataTable("Sales"); | |
/* | |
* Add columns to the newly created DataTable while specifying the column type | |
* It is important that the DataTable should have at least one column for each | |
* Smart Marker entry from the designer spreadsheet | |
*/ | |
table.Columns.Add("Year", typeof(string)); | |
table.Columns.Add("Item1", typeof(int)); | |
table.Columns.Add("Item2", typeof(int)); | |
table.Columns.Add("Item3", typeof(int)); | |
table.Columns.Add("Item4", typeof(int)); | |
table.Columns.Add("Item5", typeof(int)); | |
table.Columns.Add("Item6", typeof(int)); | |
table.Columns.Add("Item7", typeof(int)); | |
table.Columns.Add("Item8", typeof(int)); | |
table.Columns.Add("Item9", typeof(int)); | |
table.Columns.Add("Item10", typeof(int)); | |
table.Columns.Add("Item11", typeof(int)); | |
table.Columns.Add("Item12", typeof(int)); | |
// Add some rows with data to the DataTable | |
table.Rows.Add("2000", 2310, 0, 110, 15, 20, 25, 30, 1222, 200, 421, 210, 133); | |
table.Rows.Add("2005", 1508, 0, 170, 280, 190, 400, 105, 132, 303, 199, 120, 100); | |
table.Rows.Add("2010", 0, 210, 230, 140, 150, 160, 170, 110, 1999, 1229, 1120, 2300); | |
table.Rows.Add("2015", 3818, 320, 340, 260, 210, 310, 220, 0, 0, 0, 0, 122); | |
// Create an instance of WorkbookDesigner class | |
var designer = new WorkbookDesigner(); | |
// Assign the Workbook property to the instance of Workbook created in first step | |
designer.Workbook = book; | |
// Set the data source | |
designer.SetDataSource(table); | |
// Call Process method to populate data | |
designer.Process(); |
إنشاء الرسم البياني
بمجرد وضع البيانات في مكانها الصحيح ، كل ما يتعين علينا القيام به هو إنشاء مخطط بناءً على مصدر البيانات. من أجل الحفاظ على المثال بسيطًا ، سنستخدم ملحقChart.SetChartDataRangeحتى لا نضطر إلى تكوين المخطط بشكل أكبر.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook | |
var book = new Workbook(); | |
// Access the first, default Worksheet by passing its index | |
var dataSheet = book.Worksheets[0]; | |
// Name the Worksheet for later reference | |
dataSheet.Name = "ChartData"; | |
// Access the CellsCollection of first Worksheet | |
var cells = dataSheet.Cells; | |
// Insert static data (headers) | |
cells["B1"].PutValue("Item 1"); | |
cells["C1"].PutValue("Item 2"); | |
cells["D1"].PutValue("Item 3"); | |
cells["E1"].PutValue("Item 4"); | |
cells["F1"].PutValue("Item 5"); | |
cells["G1"].PutValue("Item 6"); | |
cells["H1"].PutValue("Item 7"); | |
cells["I1"].PutValue("Item 8"); | |
cells["J1"].PutValue("Item 9"); | |
cells["K1"].PutValue("Item 10"); | |
cells["L1"].PutValue("Item 11"); | |
cells["M1"].PutValue("Item 12"); | |
// Place Smart Markers | |
cells["A2"].PutValue("&=Sales.Year"); | |
cells["B2"].PutValue("&=Sales.Item1"); | |
cells["C2"].PutValue("&=Sales.Item2"); | |
cells["D2"].PutValue("&=Sales.Item3"); | |
cells["E2"].PutValue("&=Sales.Item4"); | |
cells["F2"].PutValue("&=Sales.Item5"); | |
cells["G2"].PutValue("&=Sales.Item6"); | |
cells["H2"].PutValue("&=Sales.Item7"); | |
cells["I2"].PutValue("&=Sales.Item8"); | |
cells["J2"].PutValue("&=Sales.Item9"); | |
cells["K2"].PutValue("&=Sales.Item10"); | |
cells["L2"].PutValue("&=Sales.Item11"); | |
cells["M2"].PutValue("&=Sales.Item12"); | |
// Create an instance of DataTable and name is according to the Smart Markers | |
var table = new DataTable("Sales"); | |
/* | |
* Add columns to the newly created DataTable while specifying the column type | |
* It is important that the DataTable should have at least one column for each | |
* Smart Marker entry from the designer spreadsheet | |
*/ | |
table.Columns.Add("Year", typeof(string)); | |
table.Columns.Add("Item1", typeof(int)); | |
table.Columns.Add("Item2", typeof(int)); | |
table.Columns.Add("Item3", typeof(int)); | |
table.Columns.Add("Item4", typeof(int)); | |
table.Columns.Add("Item5", typeof(int)); | |
table.Columns.Add("Item6", typeof(int)); | |
table.Columns.Add("Item7", typeof(int)); | |
table.Columns.Add("Item8", typeof(int)); | |
table.Columns.Add("Item9", typeof(int)); | |
table.Columns.Add("Item10", typeof(int)); | |
table.Columns.Add("Item11", typeof(int)); | |
table.Columns.Add("Item12", typeof(int)); | |
// Add some rows with data to the DataTable | |
table.Rows.Add("2000", 2310, 0, 110, 15, 20, 25, 30, 1222, 200, 421, 210, 133); | |
table.Rows.Add("2005", 1508, 0, 170, 280, 190, 400, 105, 132, 303, 199, 120, 100); | |
table.Rows.Add("2010", 0, 210, 230, 140, 150, 160, 170, 110, 1999, 1229, 1120, 2300); | |
table.Rows.Add("2015", 3818, 320, 340, 260, 210, 310, 220, 0, 0, 0, 0, 122); | |
// Create an instance of WorkbookDesigner class | |
var designer = new WorkbookDesigner(); | |
// Assign the Workbook property to the instance of Workbook created in first step | |
designer.Workbook = book; | |
// Set the data source | |
designer.SetDataSource(table); | |
// Call Process method to populate data | |
designer.Process(); | |
/* | |
* Save the number of rows & columns from the source DataTable in seperate variables. | |
* These values will be used later to identify the chart's data range from DataSheet | |
*/ | |
int chartRows = table.Rows.Count; | |
int chartCols = table.Columns.Count; | |
// Add a new Worksheet of type Chart to Workbook | |
int chartSheetIdx = book.Worksheets.Add(SheetType.Chart); | |
// Access the newly added Worksheet via its index | |
var chartSheet = book.Worksheets[chartSheetIdx]; | |
// Name the Worksheet | |
chartSheet.Name = "Chart"; | |
// Add a chart of type ColumnStacked to newly added Worksheet | |
int chartIdx = chartSheet.Charts.Add(ChartType.ColumnStacked, 0, 0, chartRows, chartCols); | |
// Access the newly added Chart via its index | |
var chart = chartSheet.Charts[chartIdx]; | |
// Set the data range for the chart | |
chart.SetChartDataRange(dataSheet.Name + "!A1:" + CellsHelper.ColumnIndexToName(chartCols - 1) + (chartRows + 1).ToString(), false); | |
// Set the chart to size with window | |
chart.SizeWithWindow = true; | |
// Set the format for the tick labels | |
chart.ValueAxis.TickLabels.NumberFormat = "$###,### K"; | |
// Set chart title | |
chart.Title.Text = "Sales Summary"; | |
// Set ChartSheet an active sheet | |
book.Worksheets.ActiveSheetIndex = chartSheetIdx; | |
// Save the final result | |
book.Save(dataDir + "report_out.xlsx"); |