Filtro dinámico

Escenarios de uso posibles

Cuando creas una tabla dinámica con datos conocidos y deseas filtrar la tabla dinámica, necesitas aprender y usar el filtro. Puede ayudarte a filtrar eficazmente los datos que deseas. Usando la API de Aspose.Cells Java, puedes agregar un filtro en los valores de campo en Tablas Dinámicas.

Agregar filtro en tabla dinámica en Excel

Agregar filtro en tabla dinámica en Excel, sigue estos pasos:

  1. Selecciona la Tabla Dinámica de la que deseas eliminar el filtro.
  2. Haz clic en la flecha desplegable del filtro que deseas agregar en la tabla dinámica.
  3. Selecciona “Los 10 principales” en el menú desplegable.
  4. Establece el modo de exhibición y el número de filtros.

Agregar filtro en tabla dinámica

Por favor, vea el siguiente código de muestra. Establece los datos y crea una Tabla Dinámica basada en ellos. Luego añade un filtro en el campo de fila de la tabla dinámica. Finalmente, guarda el libro de trabajo en formato XLSX de salida. Después de ejecutar el código de ejemplo, se agrega una tabla dinámica con filtro top10 a la hoja de cálculo.

Código de muestra

//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");

Borrar filtro en tabla dinámica en Excel

Limpiar filtro en Tabla Dinámica en Excel, sigue estos pasos:

  1. Selecciona la Tabla Dinámica de la que deseas eliminar el filtro.
  2. Haz clic en la flecha desplegable para el filtro que deseas borrar en la tabla dinámica.
  3. Selecciona “Limpiar filtro” en el menú desplegable.
  4. Si deseas borrar todos los filtros de la tabla dinámica, también puedes hacer clic en el botón “Limpiar filtros” en la pestaña Analizar tabla dinámica en la cinta de Excel.

Borrar filtro en tabla dinámica

Consulta el siguiente código de ejemplo. Establece los datos y crea una Tabla Dinámica basada en ello. Luego agrega un filtro en el campo de fila de la tabla dinámica. Finalmente, guarda el libro en formato XLSX de salida. Después de ejecutar el código de ejemplo, se añade un filtro top10 a la hoja de cálculo. Después de agregar un filtro, cuando necesitamos datos sin filtrar, podemos borrar el filtro en un campo de tabla dinámica específico. Después de ejecutar el código para borrar el filtro, este se eliminará en el campo de tabla dinámica específico. Consulta el XLSX de salida.

Código de muestra

//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_add.xlsx");
//Clear PivotFilter from the specific PivotField
pivotTable.getPivotFilters().clearFilter(field.getBaseIndex());
pivotTable.refreshData();
pivotTable.calculateData();
workbook.save("out_delete.xlsx");