excel 选择要应用格式的透视表的特定范围(Assose.Cells for Java)

9lowa7mx  于 2023-02-17  发布在  Java
关注(0)|答案(1)|浏览(96)

在Aspose.Cells产品中是否有与此VBA函数PivotTable.PivotSelect等效的函数?我正在寻找一种方法,可以在包含特定数据字段值的整个选定单元格上创建边框,也可以根据需要在特定范围上创建边框。
执行此操作的VBA宏如下所示(仅提供数据字段名称参数):

v_wbx.Sheets(v_SheetName).**PivotTables(v_CurrentPivotName).PivotSelect **DataFieldName**, xlDataAndLabel, True**
            Selection.Borders(xlDiagonalDown).LineStyle = xlNone
            Selection.Borders(xlDiagonalUp).LineStyle = xlNone
            With Selection.Borders(xlEdgeLeft)
                .LineStyle = xlContinuous
                .Color = v_Color
                .TintAndShade = 0
                .Weight = v_Weight
            End With
            With Selection.Borders(xlEdgeTop)
                .LineStyle = xlContinuous
                .Color = v_Color
                .TintAndShade = 0
                .Weight = v_Weight
            End With
            With Selection.Borders(xlEdgeBottom)
                .LineStyle = xlContinuous
                .Color = v_Color
                .TintAndShade = 0
                .Weight = v_Weight
            End With
            With Selection.Borders(xlEdgeRight)
                .LineStyle = xlContinuous
                .Color = v_Color
                .TintAndShade = 0
                .Weight = v_Weight
            End With

Assose.Cells是否涵盖此问题?我查阅了文档,但未找到任何解决方案。我知道Assose条件格式设置提供了“范围”选择,但在此我希望通过指定数据字段名称来创建围绕整个单元格选择的边框,并且不使用任何条件格式规则。这是通过VBA使用以下枚举完成的:https://learn.microsoft.com/en-us/office/vba/api/excel.xlptselectionmode和前面提到的函数。
我确切地说,我正在使用该产品的试用版来评估功能范围。请分享您对此主题的正面或负面反馈,谢谢

q3qa4bjr

q3qa4bjr1#

您可以通过Assose. Cells for Java设置透视表中特定数据区域值的格式。请参阅以下示例以供参考。您可以通过两种方式应用格式(即直接应用格式和通过透视格式条件)。
例如:

    • 样品代码:**
//Instantiating a Workbook object
        Workbook workbook = new Workbook();

        //Obtaining the reference of the newly added worksheet
        int sheetIndex = workbook.getWorksheets().add();
        Worksheet sheet = workbook.getWorksheets().get(sheetIndex);
        Cells cells = sheet.getCells();

        //Setting the value to the cells
        Cell cell = cells.get("A1");
        cell.setValue("Sport");
        cell = cells.get("B1");
        cell.setValue("Quarter");
        cell = cells.get("C1");
        cell.setValue("Sales");

        cell = cells.get("A2");
        cell.setValue("Golf");
        cell = cells.get("A3");
        cell.setValue("Golf");
        cell = cells.get("A4");
        cell.setValue("Tennis");
        cell = cells.get("A5");
        cell.setValue("Tennis");
        cell = cells.get("A6");
        cell.setValue("Tennis");
        cell = cells.get("A7");
        cell.setValue("Tennis");
        cell = cells.get("A8");
        cell.setValue("Golf");

        cell = cells.get("B2");
        cell.setValue("Qtr3");
        cell = cells.get("B3");
        cell.setValue("Qtr4");
        cell = cells.get("B4");
        cell.setValue("Qtr3");
        cell = cells.get("B5");
        cell.setValue("Qtr4");
        cell = cells.get("B6");
        cell.setValue("Qtr3");
        cell = cells.get("B7");
        cell.setValue("Qtr4");
        cell = cells.get("B8");
        cell.setValue("Qtr3");

        cell = cells.get("C2");
        cell.setValue(1500);
        cell = cells.get("C3");
        cell.setValue(2000);
        cell = cells.get("C4");
        cell.setValue(600);
        cell = cells.get("C5");
        cell.setValue(1500);
        cell = cells.get("C6");
        cell.setValue(4070);
        cell = cells.get("C7");
        cell.setValue(5000);
        cell = cells.get("C8");
        cell.setValue(6430);

        PivotTableCollection pivotTables = sheet.getPivotTables();

        //Adding a PivotTable to the worksheet
        int index = pivotTables.add("=A1:C8", "E3", "PivotTable2");

        //Accessing the instance of the newly added PivotTable
        PivotTable pivotTable = pivotTables.get(index);

        //Unshowing grand totals for rows.
        pivotTable.setRowGrand(false);

        //Dragging the first field to the row area.
        pivotTable.addFieldToArea(PivotFieldType.ROW, 0);

        //Dragging the second field to the column area.
        pivotTable.addFieldToArea(PivotFieldType.COLUMN, 1);

        //Dragging the third field to the data area.
        pivotTable.addFieldToArea(PivotFieldType.DATA, 2);

        pivotTable.refreshData();
        pivotTable.calculateData();

        /*
        //Apply formatting to specific data area values via Pivot format condition.
        PivotFormatConditionCollection pfcc = pivotTable.getPivotFormatConditions();
        int pIndex = pfcc.add();
        PivotFormatCondition pfc = pfcc.get(pIndex);
        FormatConditionCollection fcc = pfc.getFormatConditions();
        CellArea dataBodyRange = pivotTable.getDataBodyRange();
        fcc.addArea(dataBodyRange);
        int idx = fcc.addCondition(FormatConditionType.CELL_VALUE);
        FormatCondition fc = fcc.get(idx);
        fc.setFormula1("6000");
        fc.setOperator(OperatorType.GREATER_OR_EQUAL);
        //fc.getStyle().setBackgroundColor(com.aspose.cells.Color.getRed());
        fc.getStyle().setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
        fc.getStyle().setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
        fc.getStyle().setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
        fc.getStyle().setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
        */
        ///*
        //Apply formatting directly to specific data fields. 
        CellArea dataArea = pivotTable.getDataBodyRange();
        for(int dataRowNum = dataArea.StartRow; dataRowNum <= dataArea.EndRow;dataRowNum++){
            for(int dataColNum = dataArea.StartColumn;dataColNum <= dataArea.EndColumn;dataColNum++){
                cell = cells.get(dataRowNum,dataColNum);
                int value = cell.getIntValue();
                System.out.println(value);
                if (value > 6000) {
                Style style = cell.getStyle();
                com.aspose.cells.Font font = style.getFont();
                font.setColor(com.aspose.cells.Color.getBlue());
                style.setBorder(BorderType.LEFT_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
                style.setBorder(BorderType.TOP_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
                style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
                style.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THICK, com.aspose.cells.Color.getRed());
                pivotTable.format(dataRowNum, dataColNum, style);

                }
            }
        }
        workbook.save("f:\\files\\out1.xlsx");

您也可以在专用的forum中发布您的查询。
ps.我在aspose做支持开发人员/布道者。

相关问题