Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
A pivot table that lists every item of a row field can be hard to read when the field has dozens of members. A Top 10 report keeps only the highest or lowest ranked items, which is exactly what ranking reports need — “top 5 products by revenue” or “bottom 3 regions by sales count”. Aspose.Cells supports two mechanisms for this, and they correspond to two generations of Microsoft Excel.
PivotField.Top10AutoShow() method. This is the classic “Show the top/bottom N items” setting stored directly on the pivot field. It works in every Excel version, old and new, because it is part of the original pivot table format.PivotField.FilterTop10() method. This is the modern Top 10 value filter found under the field menu’s Value Filters. It is stored as a PivotFilter object attached to the field and offers item-count and percentage based ranking.The classic AutoShow is configured with a single call to PivotField.Top10AutoShow(bool isAutoShow, bool isTop, int32_t count, int32_t rankBy):
isAutoShow — enables or disables the AutoShow setting on the field.isTop — true keeps the highest ranked items (Top), false keeps the lowest (Bottom).count — how many items to keep, for example 10 for a classic Top 10 report.rankBy — the zero-based index of the data field whose values rank the items. Pass -1 to rank the items by the field itself.
The ranking can be driven by a value field or by the field itself. Two natural variants exist, shown below.Rank the row field’s items by an ordinary value field such as Sum of Sales. The following example builds a pivot table with one row field (Sport) and one data field (Sales), then shows the top 2 sports by sales. rankBy is 0 because “Sum of Sales” is the data field at index 0.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet worksheet = workbook.GetWorksheets().Get(0);
worksheet.GetCells().Get(0, 0).PutValue(u"Sport");
worksheet.GetCells().Get(0, 1).PutValue(u"Quarter");
worksheet.GetCells().Get(0, 2).PutValue(u"Sales");
worksheet.GetCells().Get(1, 0).PutValue(u"Golf");
worksheet.GetCells().Get(1, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(1, 2).PutValue(2554);
worksheet.GetCells().Get(2, 0).PutValue(u"Golf");
worksheet.GetCells().Get(2, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(2, 2).PutValue(3331);
worksheet.GetCells().Get(3, 0).PutValue(u"Golf");
worksheet.GetCells().Get(3, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(3, 2).PutValue(4108);
worksheet.GetCells().Get(4, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(4, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(4, 2).PutValue(4885);
worksheet.GetCells().Get(5, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(5, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(5, 2).PutValue(5662);
worksheet.GetCells().Get(6, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(6, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(6, 2).PutValue(6439);
worksheet.GetCells().Get(7, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(7, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(7, 2).PutValue(7216);
worksheet.GetCells().Get(8, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(8, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(8, 2).PutValue(7993);
worksheet.GetCells().Get(9, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(9, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(9, 2).PutValue(8770);
worksheet.GetCells().Get(10, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(10, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(10, 2).PutValue(9547);
worksheet.GetCells().Get(11, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(11, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(11, 2).PutValue(10324);
worksheet.GetCells().Get(12, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(12, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(12, 2).PutValue(11101);
worksheet.GetCells().Get(13, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(13, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(13, 2).PutValue(11878);
worksheet.GetCells().Get(14, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(14, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(14, 2).PutValue(12655);
worksheet.GetCells().Get(15, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(15, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(15, 2).PutValue(13432);
PivotTable pivotTable = worksheet.GetPivotTables().Add(u"A1:C16", 2, 5);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Sport");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Sales");
pivotTable.CalculateData();
PivotField sportField = pivotTable.GetRowFields().Get(0);
// Show the top 2 items ranked by "Sum of Sales" (data field index 0)
sportField.Top10AutoShow(true, true, 2, 0);
pivotTable.CalculateData();
workbook.Save(u"output-top10-by-value-field.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
A field can also be ranked by itself — pass -1 as rankBy. No extra data field is needed for this variant; the field only has to sit in the row (or column) area. The following example shows the bottom 3 sports ranked by the field itself. When this setting is saved, the rankBy attribute is omitted from the pivot table XML because -1 is the format’s default value, and GetTop10AutoShowSettings().GetRankBy() returns -1.
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet worksheet = workbook.GetWorksheets().Get(0);
worksheet.GetCells().Get(0, 0).PutValue(u"Sport");
worksheet.GetCells().Get(0, 1).PutValue(u"Quarter");
worksheet.GetCells().Get(0, 2).PutValue(u"Sales");
worksheet.GetCells().Get(1, 0).PutValue(u"Golf");
worksheet.GetCells().Get(1, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(1, 2).PutValue(2554);
worksheet.GetCells().Get(2, 0).PutValue(u"Golf");
worksheet.GetCells().Get(2, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(2, 2).PutValue(3331);
worksheet.GetCells().Get(3, 0).PutValue(u"Golf");
worksheet.GetCells().Get(3, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(3, 2).PutValue(4108);
worksheet.GetCells().Get(4, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(4, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(4, 2).PutValue(4885);
worksheet.GetCells().Get(5, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(5, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(5, 2).PutValue(5662);
worksheet.GetCells().Get(6, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(6, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(6, 2).PutValue(6439);
worksheet.GetCells().Get(7, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(7, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(7, 2).PutValue(7216);
worksheet.GetCells().Get(8, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(8, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(8, 2).PutValue(7993);
worksheet.GetCells().Get(9, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(9, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(9, 2).PutValue(8770);
worksheet.GetCells().Get(10, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(10, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(10, 2).PutValue(9547);
worksheet.GetCells().Get(11, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(11, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(11, 2).PutValue(10324);
worksheet.GetCells().Get(12, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(12, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(12, 2).PutValue(11101);
worksheet.GetCells().Get(13, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(13, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(13, 2).PutValue(11878);
worksheet.GetCells().Get(14, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(14, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(14, 2).PutValue(12655);
worksheet.GetCells().Get(15, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(15, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(15, 2).PutValue(13432);
PivotTable pivotTable = worksheet.GetPivotTables().Add(u"A1:C16", 2, 5);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Sport");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Sales");
pivotTable.CalculateData();
PivotField sportField = pivotTable.GetRowFields().Get(0);
// Show the bottom 3 items ranked by the field itself (rankBy = -1)
sportField.Top10AutoShow(true, false, 3, -1);
pivotTable.CalculateData();
workbook.Save(u"output-top10-by-field-itself.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
The PivotField.GetTop10AutoShowSettings() method returns a PivotFieldTop10AutoShowSettings object that exposes the AutoShow options currently stored on the field. Use it to inspect how an existing workbook, or a pivot table you just configured, will present its items — including workbooks loaded from file.
#include "Aspose.Cells.h"
#include <iostream>
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Aspose::Cells::Startup();
Workbook workbook(u"output-top10-by-value-field.xlsx");
PivotTable pivotTable = workbook.GetWorksheets().Get(0).GetPivotTables().Get(0);
PivotField sportField = pivotTable.GetRowFields().Get(0);
PivotFieldTop10AutoShowSettings settings = sportField.GetTop10AutoShowSettings();
std::cout << "AutoShow enabled: " << settings.IsAutoShow() << std::endl;
std::cout << "Direction: " << (settings.IsTop() ? "Top" : "Bottom") << std::endl;
std::cout << "Item count: " << settings.GetAutoShowCount() << std::endl;
std::cout << "Ranking data field index: " << settings.GetRankBy() << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
The four members map to the classic AutoShow attributes written into the pivot table XML (autoShow, topAutoShow, itemPageCount, rankBy), so the values read back are the values every Excel version will honor.
PivotField properties IsAutoShow, IsAscendShow, GetAutoShowCount and GetAutoShowField are deprecated. Use the PivotFieldTop10AutoShowSettings object instead: IsAutoShow becomes GetTop10AutoShowSettings().IsAutoShow(), IsAscendShow becomes GetTop10AutoShowSettings().IsTop(), GetAutoShowCount becomes GetTop10AutoShowSettings().GetAutoShowCount(), and GetAutoShowField becomes GetTop10AutoShowSettings().GetRankBy(). The Top10AutoShow() method itself is the supported way to change the settings.Top10AutoShow works with row and column fields. A field that is only in the page (filter) area cannot be ranked this way because its items are not laid out in the pivot body.Modern Excel versions expose Top 10 as a value filter on the field menu (Value Filters, Top 10…). Aspose.Cells models it with the PivotField.FilterTop10(int32_t valueFieldIndex, PivotFilterType type, bool isTop, int32_t itemCount) method, which returns the created PivotFilter:
valueFieldIndex — the zero-based index of the data field whose aggregated values rank the items.type — what itemCount means. PivotFilterType::Count keeps a fixed number of items (the dialog’s Items option). PivotFilterType::Percent keeps a percentage of the items, so itemCount is the percentage itself — 30 keeps the top 30 percent. PivotFilterType::Sum ranks by cumulative value coverage.isTop — true for Top, false for Bottom.itemCount — the N in Top N; its unit depends on type.#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
Aspose::Cells::Startup();
Workbook workbook;
Worksheet worksheet = workbook.GetWorksheets().Get(0);
worksheet.GetCells().Get(0, 0).PutValue(u"Sport");
worksheet.GetCells().Get(0, 1).PutValue(u"Quarter");
worksheet.GetCells().Get(0, 2).PutValue(u"Sales");
worksheet.GetCells().Get(1, 0).PutValue(u"Golf");
worksheet.GetCells().Get(1, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(1, 2).PutValue(2554);
worksheet.GetCells().Get(2, 0).PutValue(u"Golf");
worksheet.GetCells().Get(2, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(2, 2).PutValue(3331);
worksheet.GetCells().Get(3, 0).PutValue(u"Golf");
worksheet.GetCells().Get(3, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(3, 2).PutValue(4108);
worksheet.GetCells().Get(4, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(4, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(4, 2).PutValue(4885);
worksheet.GetCells().Get(5, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(5, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(5, 2).PutValue(5662);
worksheet.GetCells().Get(6, 0).PutValue(u"Tennis");
worksheet.GetCells().Get(6, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(6, 2).PutValue(6439);
worksheet.GetCells().Get(7, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(7, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(7, 2).PutValue(7216);
worksheet.GetCells().Get(8, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(8, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(8, 2).PutValue(7993);
worksheet.GetCells().Get(9, 0).PutValue(u"Rugby");
worksheet.GetCells().Get(9, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(9, 2).PutValue(8770);
worksheet.GetCells().Get(10, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(10, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(10, 2).PutValue(9547);
worksheet.GetCells().Get(11, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(11, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(11, 2).PutValue(10324);
worksheet.GetCells().Get(12, 0).PutValue(u"Cricket");
worksheet.GetCells().Get(12, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(12, 2).PutValue(11101);
worksheet.GetCells().Get(13, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(13, 1).PutValue(u"Qtr1");
worksheet.GetCells().Get(13, 2).PutValue(11878);
worksheet.GetCells().Get(14, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(14, 1).PutValue(u"Qtr2");
worksheet.GetCells().Get(14, 2).PutValue(12655);
worksheet.GetCells().Get(15, 0).PutValue(u"Boxing");
worksheet.GetCells().Get(15, 1).PutValue(u"Qtr3");
worksheet.GetCells().Get(15, 2).PutValue(13432);
PivotTable pivotTable = worksheet.GetPivotTables().Add(u"A1:C16", 2, 5);
pivotTable.AddFieldToArea(PivotFieldType::Row, u"Sport");
pivotTable.AddFieldToArea(PivotFieldType::Data, u"Sales");
pivotTable.CalculateData();
PivotField sportField = pivotTable.GetRowFields().Get(0);
// Top 10 Filter — keep the top 2 items by "Sum of Sales" (data field index 0)
PivotFilter filter = sportField.FilterTop10(0, PivotFilterType::Count, true, 2);
// For a percentage-based filter instead, keep the top 30 percent of the items:
// sportField.FilterTop10(0, PivotFilterType::Percent, true, 30);
pivotTable.GetPivotCache().Refresh();
pivotTable.CalculateData();
workbook.Save(u"output-top10-filter.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
Top10AutoShow and FilterTop10 are two separate features. AutoShow writes the classic autoShow attribute set on the pivot field and is honored by all Excel versions back to Excel 97. FilterTop10 writes a modern PivotFilter entry, which requires Excel 2007 or later. When a workbook is opened in an older Excel version, a pivot table that uses the modern filter falls back to showing all items. For documents that must round-trip through legacy tooling, prefer Top10AutoShow; for modern workbooks, prefer FilterTop10 because it composes with the other value filters.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.