Formattazione della tabella pivot

Aspetto della tabella pivot

Come creare una tabella pivot spiega come creare una semplice tabella pivot. Questo articolo descrive come personalizzare l’aspetto di una tabella pivot impostando diverse proprietà:

  • Opzioni di formato tabella pivot
  • Opzioni di formato dei campi pivot
  • Opzioni di formato del campo dati

Come impostare le opzioni di formato tabella pivot

La classe PivotTable controlla complessivamente la tabella pivot e può essere formattata in vari modi.

Come impostare il tipo di autoformato

Microsoft Excel offre diversi formati di rapporto preimpostati. Aspose.Cells for Node.js via C++ supporta anche queste opzioni di formattazione. Per accedervi:

  1. Imposta PivotTable.setIsAutoFormat(value) su vero.
  2. Assegna un’opzione di formattazione dall’enumerazione PivotTableAutoFormatType.
const AsposeCells = require("aspose.cells.node");
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
//The path to the documents directory.
var dataDir = RunExamples.GetDataDir(".");
//Load a template file
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls");
var pivotindex = 0;
//Get the first worksheet
var worksheet = workbook.getWorksheets().get(0);
//Accessing the PivotTable
var pivotTable = worksheet.getPivotTables().get(pivotindex);
//Setting the PivotTable report is automatically formatted
pivotTable.setIsAutoFormat(true);
//Setting the PivotTable atuoformat type.
pivotTable.setAutoFormatType(AsposeCells.PivotTableAutoFormatType.Report5);
//Saving the Excel file
workbook.save(dataDir + "output.xls");

Come impostare le opzioni di formattazione

Il codice di esempio qui sotto mostra come formattare la tabella pivot per mostrare i totali generali per righe e colonne, e come impostare l’ordine dei campi del report. Mostra inoltre come impostare una stringa personalizzata per i valori nulli.

const AsposeCells = require("aspose.cells.node");
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
//The path to the documents directory.
var dataDir = RunExamples.GetDataDir(".");
//Load a template file
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls");
//Get the first worksheet
var worksheet = workbook.getWorksheets().get(0);
var pivotindex = 0;
//Accessing the PivotTable
var pivotTable = worksheet.getPivotTables().get(pivotindex);
//Setting the PivotTable report shows grand totals for rows.
pivotTable.setRowGrand(true);
//Setting the PivotTable report shows grand totals for columns.
pivotTable.setColumnGrand(true);
//Setting the PivotTable report displays a custom string in cells that contain null values.
pivotTable.setDisplayNullString(true);
pivotTable.setNullString("null");
//Setting the PivotTable report's layout
pivotTable.setPageFieldOrder(AsposeCells.PrintOrderType.DownThenOver);
//Saving the Excel file
workbook.save(dataDir + "output.xls");

Formattazione Aspetto e Sensazione Manualmente

Per formattare manualmente l’aspetto del report della tabella pivot, anziché utilizzare formati di report predefiniti, utilizzare i metodi PivotTable.formatAll(style) e PivotTable.format(row, column, style). Creare un oggetto stile per la formattazione desiderata, ad esempio:

const AsposeCells = require("aspose.cells.node");
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
//The path to the documents directory.
var dataDir = RunExamples.GetDataDir(".")
//Load a template file
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls");
//Get the first worksheet
var worksheet = workbook.getWorksheets().get(0);
var pivot = worksheet.getPivotTables().get(0);
pivot.setPivotTableStyleType(AsposeCells.PivotTableStyleType.PivotTableStyleDark1);
var style = workbook.createStyle();
style.getFont().setName("Arial Black");
style.setPattern(AsposeCells.BackgroundType.Solid);
style.setForegroundColor(AsposeCells.Color.Yellow);
pivot.formatAll(style);
//Saving the Excel file
workbook.save(dataDir + "output.xls");

Come impostare le opzioni di formato campo pivot

La classe PivotField rappresenta un campo in una tabella pivot e può essere formattata in vari modi. Il codice di esempio qui sotto mostra come:

  • Accedere ai campi di riga.
  • Impostare i subtotali.
  • Impostazione dell’ordinamento automatico.
  • Impostazione dell’autovisualizzazione.

Come impostare il formato dei campi riga/colonna/pagina

const AsposeCells = require("aspose.cells.node");
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
//The path to the documents directory.
var dataDir = RunExamples.GetDataDir(".");
//Load a template file
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls");
//Get the first worksheet
var worksheet = workbook.getWorksheets().get(0);
var pivotindex = 0;
//Accessing the PivotTable
var pivotTable = worksheet.getPivotTables().get(pivotindex);
//Setting the PivotTable report shows grand totals for rows.
pivotTable.setRowGrand(true);
//Accessing the row fields.
var pivotFields = pivotTable.getRowFields();
//Accessing the first row field in the row fields.
var pivotField = pivotFields.get(0);
//Setting Subtotals.
pivotField.setSubtotals(AsposeCells.PivotFieldSubtotalType.Sum, true);
pivotField.setSubtotals(AsposeCells.PivotFieldSubtotalType.Count, true);
//Setting autosort options.
//Setting the field auto sort.
pivotField.setIsAutoSort(true);
//Setting the field auto sort ascend.
pivotField.setIsAscendSort(true);
//Setting the field auto sort using the field itself.
pivotField.setAutoSortField(-5);
//Setting autoShow options.
//Setting the field auto show.
pivotField.setIsAutoShow(true);
//Setting the field auto show ascend.
pivotField.setIsAscendShow(false);
//Setting the auto show using field(data field).
pivotField.setAutoShowField(0);
//Saving the Excel file
workbook.save(dataDir + "output.xls");

Come impostare il formato dei campi dati

Il campione di codice sottostante mostra come impostare i formati di visualizzazione e il formato numerico per i campi dati.

const AsposeCells = require("aspose.cells.node");
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
//The path to the documents directory.
var dataDir = RunExamples.GetDataDir(".");
//Load a template file
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls");
//Get the first worksheet
var worksheet = workbook.getWorksheets().get(0);
var pivotindex = 0;
//Accessing the PivotTable
var pivotTable = worksheet.getPivotTables().get(pivotindex);
//Accessing the data fields.
var pivotFields = pivotTable.getDataFields();
//Accessing the first data field in the data fields.
var pivotField = pivotFields.get(0);
//Setting data display format
pivotField.showValuesAs(AsposeCells.PivotFieldDataDisplayFormat.PercentageOf, 1, AsposeCells.PivotItemPositionType.Next, 0);
//Setting number format
pivotField.setNumber(10);
//Saving the Excel file
workbook.save(dataDir + "output.xls");

Come eliminare i campi di un Pivot

Il PivotFieldCollection ha un metodo chiamato clear() che ti consente di eliminare i campi di un Pivot. Usalo quando vuoi eliminare tutti i campi di un Pivot nelle aree, ad esempio, pagina, colonna, riga o dati. Il campione di codice sottostante mostra come eliminare tutti i campi di un Pivot in un’area dati.

const AsposeCells = require("aspose.cells.node");
//For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
//The path to the documents directory.
var dataDir = RunExamples.GetDataDir(".")
//Load a template file
var workbook = new AsposeCells.Workbook(dataDir + "Book1.xls");
//Get the first worksheet
var sheet = workbook.getWorksheets().get(0);
//Get the pivot tables in the sheet
var pivotTables = sheet.getPivotTables();
//Get the first PivotTable
var pivotTable = pivotTables.get(0);
//Clear all the data fields
pivotTable.getDataFields().clear();
//Add new data field
pivotTable.addFieldToArea(AsposeCells.PivotFieldType.Data, "Betrag Netto FW");
//Set the refresh data flag on
pivotTable.setRefreshDataFlag(true);
//Refresh and calculate the pivot table data
pivotTable.refreshData();
pivotTable.calculateData();
pivotTable.setRefreshDataFlag(false);
//Saving the Excel file
workbook.save(dataDir + "output.xls");