简体   繁体   中英

Java Excel Pivot Table Field Conditional Formatting

Please, could you advise, how to set up a Conditional formatting related to the MS Excel Pivot Table Data Field (not to the Excel sheet cell.). The Excel pivot table is created via Java Apache Poi. I need a pure Java solution either via the Apache Poi library or via Spire.XLS or Aspose library. I prefer open source solution but if there is no other option I would welcome paid library if the solution works well. I failed searching this via Google Worldwide. Thank you very much. Miroslav

Aspose.Cells for Java can serve your needs. You may add conditional formattings to pivot table fields (row, column, data, etc.).

There are relevant APIs (method overloads, etc.) for the purpose:

  • PivotFormatCondition.addDataAreaCondition(string fieldName) method which adds PivotTable conditional format limit in the data fields.

  • PivotFormatCondition.addDataAreaCondition(PivotField dataField) method which adds PivotTable conditional format limit in the data fields.

  • PivotFormatCondition.addRowAreaCondition(string fieldName) method which adds PivotTable conditional format range limit in the row fields.

  • PivotFormatCondition.addRowAreaCondition(PivotField rowField) method which adds PivotTable conditional format range limit in the row fields.

  • PivotFormatCondition.addColumnAreaCondition(string fieldName) method which adds PivotTable conditional format range limit in the column fields.

  • PivotFormatCondition.addColumnAreaCondition(PivotField columnField) method which adds PivotTable conditional format range limit in the column fields.

  • PivotFormatCondition.setConditionalAreas() method which sets conditional areas of PivotFormatCondition object.

eg

Sample code:

        String filePath = "........";
        Workbook workbook = new Workbook(filePath + "Book1.xlsx");
        //Get the first pivot table in the first worksheet.
        PivotTable pivot = workbook.getWorksheets().get(0).getPivotTables().get(0);

        PivotFormatConditionCollection pfcs = pivot.getPivotFormatConditions();
        //clear all the current conditional formats (if required).
        pfcs.clear();
        workbook.getWorksheets().get(0).getConditionalFormattings().clear();

        //Add new conditional formatting
        int pIndex = pfcs.add();
        PivotFormatCondition pfc = pfcs.get(pIndex);
        pfc.setScopeType(PivotConditionFormatScopeType.FIELD);

        //Sample code 1:
        pfc.addDataAreaCondition("myDataField1");
        pfc.addRowAreaCondition("myRowField");
        //Or
        //Sample code 2:
        // pfc.addDataAreaCondition(pivot.DataFields[2]);
        // pfc.addRowAreaCondition(pivot.RowFields[0]);

        pfc.setConditionalAreas();

        FormatConditionCollection fcc = pfc.getFormatConditions();
        int idx = fcc.addCondition(FormatConditionType.CELL_VALUE);
        FormatCondition fc = fcc.get(idx);
        fc.setFormula1("0.4");
        fc.setOperator(OperatorType.GREATER_OR_EQUAL);
        fc.getStyle().setBackgroundColor(com.aspose.cells.Color.getRed());

        workbook.save(filePath + "out.xlsx");
        workbook.save(filePath + "out.pdf");

You may also post your queries or comments in other forums .

PS. I am working as Support developer/ Evangelist at Aspose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM