Formatage du tableau croisé dynamique
Apparence du tableau croisé dynamique
Comment créer un tableau croisé dynamique a montré comment créer un tableau croisé dynamique simple. Cet article va plus loin et discute de la personnalisation de l’apparence d’un tableau croisé dynamique en définissant des propriétés.
Configurer les options de formatage du tableau croisé dynamique
La classe PivotTable vous permet de définir diverses options de formatage pour un tableau croisé dynamique.
Définition des types de mise en forme automatique et de style de tableau croisé dynamique
L’exemple de code suivant illustre comment définir le type de mise en forme automatique et le type de style de tableau croisé dynamique en utilisant les propriétés AutoFormatType et PivotTableStyleType.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SetAutoFormatandPivotTableStyleTypes.class) + "PivotTables/"; | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "PivotTable.xls"); | |
int pivotindex = 0; | |
// Get the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(pivotindex); | |
// Accessing the PivotTable | |
PivotTable pivotTable = worksheet.getPivotTables().get(pivotindex); | |
//Setting the PivotTable report is automatically formatted for Excel 2003 formats | |
pivotTable.setAutoFormat(true); | |
//Setting the PivotTable atuoformat type. | |
pivotTable.setAutoFormatType(PivotTableAutoFormatType.CLASSIC); | |
//Setting the PivotTable's Styles for Excel 2007/2010 formats e.g XLSX. | |
pivotTable.setPivotTableStyleType(PivotTableStyleType.PIVOT_TABLE_STYLE_LIGHT_1); |
Paramétrage des options de formatage
L’exemple de code suivant illustre comment définir un certain nombre d’options de formatage pour un rapport de tableau croisé dynamique, y compris l’ajout de totaux généraux pour les lignes et les colonnes.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingFormatOptions.class) + "PivotTables/"; | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "PivotTable.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Dragging the third field to the data area. | |
pivotTable.addFieldToArea(PivotFieldType.DATA, 2); | |
// Show grand totals for rows. | |
pivotTable.setRowGrand(true); | |
// Show grand totals for columns. | |
pivotTable.setColumnGrand(true); | |
// Display a custom string in cells that contain null values. | |
pivotTable.setDisplayNullString(true); | |
pivotTable.setNullString("null"); | |
// Setting the layout | |
pivotTable.setPageFieldOrder(PrintOrderType.DOWN_THEN_OVER); |
Configurer les options de formatage des champs de tableau croisé dynamique
En plus de contrôler le formatage du tableau croisé dynamique global, Aspose.Cells for Java permet un contrôle précis du formatage des champs de ligne, des champs de colonne et des champs de page.
Format des Champs de Ligne, Colonne et Page
L’exemple de code suivant montre comment accéder aux champs de ligne, accéder à une ligne particulière, définir des sous-totaux, appliquer un tri automatique et utiliser l’option autoShow.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SetRowColumnPageFieldsFormat.class) + "PivotTables/"; | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "PivotTable.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Accessing the row fields. | |
PivotFieldCollection pivotFields = pivotTable.getRowFields(); | |
// Accessing the first row field in the row fields. | |
PivotField pivotField = pivotFields.get(0); | |
// Setting Subtotals. | |
pivotField.setSubtotals(PivotFieldSubtotalType.SUM, true); | |
pivotField.setSubtotals(PivotFieldSubtotalType.COUNT, true); | |
// Setting autosort options. Setting the field auto sort. | |
pivotField.setAutoSort(true); | |
// Setting the field auto sort ascend. | |
pivotField.setAscendSort(true); | |
// Setting the field auto sort using the field itself. | |
pivotField.setAutoSortField(-1); | |
// Setting autoShow options. Setting the field auto show. | |
pivotField.setAutoShow(true); | |
// Setting the field auto show ascend. | |
pivotField.setAscendShow(false); | |
// Setting the auto show using field(data field). | |
pivotField.setAutoShowField(0); |
Formatage des Champs de Données
Les lignes de code suivantes illustrent comment formater les champs de données.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingDataFieldFormat.class) + "PivotTables/"; | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "PivotTable.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Accessing the data fields. | |
PivotFieldCollection pivotFields = pivotTable.getDataFields(); | |
// Accessing the first data field in the data fields. | |
PivotField pivotField = pivotFields.get(0); | |
// Setting data display format | |
pivotField.setDataDisplayFormat(PivotFieldDataDisplayFormat.PERCENTAGE_OF); | |
// Setting the base field. | |
pivotField.setBaseFieldIndex(1); | |
// Setting the base item. | |
pivotField.setBaseItemPosition(PivotItemPosition.NEXT); | |
// Setting number format | |
pivotField.setNumber(10); |
Modifier le Style Rapide d’un Tableau Croisé Dynamique
Les exemples de code suivants montrent comment modifier le style rapide appliqué à un tableau croisé dynamique.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ModifyPivotTableQuickStyle.class) + "PivotTables/"; | |
// Open the template file containing the pivot table. | |
Workbook wb = new Workbook(dataDir + "sample1.xlsx"); | |
// Add Pivot Table style | |
Style style1 = wb.createStyle(); | |
com.aspose.cells.Font font1 = style1.getFont(); | |
font1.setColor(Color.getRed()); | |
Style style2 = wb.createStyle(); | |
com.aspose.cells.Font font2 = style2.getFont(); | |
font2.setColor(Color.getBlue()); | |
int i = wb.getWorksheets().getTableStyles().addPivotTableStyle("tt"); | |
// Get and Set the table style for different categories | |
TableStyle ts = wb.getWorksheets().getTableStyles().get(i); | |
int index = ts.getTableStyleElements().add(TableStyleElementType.FIRST_COLUMN); | |
TableStyleElement e = ts.getTableStyleElements().get(index); | |
e.setElementStyle(style1); | |
index = ts.getTableStyleElements().add(TableStyleElementType.GRAND_TOTAL_ROW); | |
e = ts.getTableStyleElements().get(index); | |
e.setElementStyle(style2); | |
// Set Pivot Table style name | |
PivotTable pt = wb.getWorksheets().get(0).getPivotTables().get(0); | |
pt.setPivotTableStyleName("tt"); | |
// Save the file. | |
wb.save(dataDir + "ModifyPivotTableQuickStyle_out.xlsx"); |
Effacement des Champs de Tableau Croisé Dynamique
PivotFieldCollection a une méthode nommée clear() qui efface les champs de tableau croisé dynamique. Utilisez-la pour effacer les champs de tableau croisé dynamique dans toutes les zones, par exemple, page, colonne, ligne ou données. L’exemple de code ci-dessous montre comment effacer tous les champs de tableau croisé dynamique dans la zone de données.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ClearPivotFields.class) + "PivotTables/"; | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "PivotTable.xls"); | |
// Get the first worksheet | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
// Get the pivot tables in the sheet | |
PivotTableCollection pivotTables = sheet.getPivotTables(); | |
// Get the first PivotTable | |
PivotTable pivotTable = pivotTables.get(0); | |
// Clear all the data fields | |
pivotTable.getDataFields().clear(); | |
// Add new data field | |
pivotTable.addFieldToArea(PivotFieldType.DATA, "Betrag Netto FW"); | |
// Set the refresh data flag on | |
pivotTable.setRefreshDataFlag(false); | |
// Refresh and calculate the pivot table data | |
pivotTable.refreshData(); | |
pivotTable.calculateData(); | |
// Save the Excel file | |
workbook.save(dataDir + "ClearPivotFields_out.xlsx"); |
Fonction de consolidation
Application de la fonction de consolidation aux champs de données d’un tableau croisé dynamique
Aspose.Cells peut être utilisé pour appliquer la fonction de consolidation aux champs de données (ou aux champs de valeur) du tableau croisé dynamique. Dans Microsoft Excel, vous pouvez cliquer avec le bouton droit sur le champ de valeur, puis sélectionner l’option Paramètres du champ de valeur… et ensuite sélectionner l’onglet Résumer les valeurs par. À partir de là, vous pouvez sélectionner n’importe quelle fonction de consolidation de votre choix, comme Somme, Compte, Moyenne, Max, Min, Produit, Comptage distinct, etc.
Aspose.Cells fournit une énumération ConsolidationFunction pour prendre en charge les fonctions de consolidation suivantes.
- ConsolidationFunction.AVERAGE
- ConsolidationFunction.COUNT
- ConsolidationFunction.COUNT_NUMS
- ConsolidationFunction.DISTINCT_COUNT
- ConsolidationFunction.MAX
- ConsolidationFunction.MIN
- ConsolidationFunction.PRODUCT
- ConsolidationFunction.STD_DEV
- ConsolidationFunction.STD_DEVP
- ConsolidationFunction.SUM
- ConsolidationFunction.VAR
- ConsolidationFunction.VARP
Le code suivant applique la fonction de consolidation Moyenne au premier champ de données (ou champ de valeur) et la fonction de consolidation ComptageDistinct au deuxième champ de données (ou champ de valeur).
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ConsolidationFunctions.class) + "PivotTables/"; | |
// Create workbook from source excel file | |
Workbook workbook = new Workbook(dataDir + "sample1.xlsx"); | |
// Access the first worksheet of the workbook | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access the first pivot table of the worksheet | |
PivotTable pivotTable = worksheet.getPivotTables().get(0); | |
// Apply Average consolidation function to first data field | |
pivotTable.getDataFields().get(0).setFunction(ConsolidationFunction.AVERAGE); | |
// Apply DistinctCount consolidation function to second data field | |
pivotTable.getDataFields().get(1).setFunction(ConsolidationFunction.DISTINCT_COUNT); | |
// Calculate the data to make changes affect | |
pivotTable.calculateData(); | |
// Save the workbook | |
workbook.save(dataDir + "ConsolidationFunctions_out.xlsx"); |