إضافة فلتر في جدول الدوران
سيناريوهات الاستخدام المحتملة
عند إنشاء جدول دوران ببيانات معروفة وترغب في تصفية جدول الدوران، تحتاج إلى تعلم واستخدام الفلتر. يمكن أن يساعدك في تصفية البيانات التي تريد بشكل فعال. من خلال استخدام واجهة برمجة تطبيقات Aspose.Cells لجافا، يمكنك إضافة فلتر على قيم الحقول في جداول الدوران.
إضافة فلتر في جدول الدوران
يرجى الاطلاع على الكود النموذجي التالي. يقوم بتعيين البيانات وإنشاء PivotTable استنادًا إليها. ثم إضافة فلتر على حقل الصف من جدول الدوران. أخيرًا، يقوم بحفظ دفتر العمل في تنسيق XLSX الناتج. بعد تنفيذ كود المثال، يتم إضافة جدول دوران مع فلتر top10 إلى ورقة العمل.
الكود المثالي
//Instantiating an Workbook object | |
Workbook workbook = new Workbook(); | |
//Obtaining the reference of the newly added worksheet | |
Worksheet ws = workbook.getWorksheets().get(0); | |
Cells cells = ws.getCells(); | |
//Setting the value to the cells | |
Cell cell = cells.get("A1"); | |
cell.putValue("Fruit"); | |
cell = cells.get("B1"); | |
cell.putValue("Count"); | |
cell = cells.get("A2"); | |
cell.putValue("Apple"); | |
cell = cells.get("A3"); | |
cell.putValue("Mango"); | |
cell = cells.get("A4"); | |
cell.putValue("Blackberry"); | |
cell = cells.get("A5"); | |
cell.putValue("Cherry"); | |
cell = cells.get("A6"); | |
cell.putValue("Guava"); | |
cell = cells.get("A7"); | |
cell.putValue("Carambola"); | |
cell = cells.get("A8"); | |
cell.putValue("Banana"); | |
cell = cells.get("B2"); | |
cell.putValue(5); | |
cell = cells.get("B3"); | |
cell.putValue(3); | |
cell = cells.get("B4"); | |
cell.putValue(6); | |
cell = cells.get("B5"); | |
cell.putValue(4); | |
cell = cells.get("B6"); | |
cell.putValue(5); | |
cell = cells.get("B7"); | |
cell.putValue(2); | |
cell = cells.get("B8"); | |
cell.putValue(20); | |
//Adding a PivotTable to the worksheet | |
int i = ws.getPivotTables().add("=A1:B8", "D10", "PivotTable1"); | |
//Accessing the instance of the newly added PivotTable | |
PivotTable pivotTable = ws.getPivotTables().get(i); | |
pivotTable.addFieldToArea(PivotFieldType.ROW, 0); | |
pivotTable.addFieldToArea(PivotFieldType.DATA, "Count"); | |
pivotTable.getDataFields().get(0).setFunction(ConsolidationFunction.SUM); | |
PivotField field = pivotTable.getRowFields().get(0); | |
field.setAutoSort(true); | |
field.setAscendSort(false); | |
field.setAutoSortField(0); | |
//Add top10 filter | |
PivotField filterField = pivotTable.getRowFields().get(0); | |
filterField.filterTop10(0, PivotFilterType.COUNT, false, 5); | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
workbook.save("out.xlsx"); |