Formatera Pivot tabell

Pivottabellens utseende

Hur man skapar en pivottabell förklarar hur man skapar en enkel pivottabell. Den här artikeln beskriver hur man anpassar en pivottabells utseende genom att ställa in olika egenskaper:

  • Pivottabellformatalternativ
  • Alternativ för pivottabellfältformat
  • Alternativ för datafältformat

Hur man ställer in pivottabellsformatalternativ

Klassen PivotTable kontrollerar den övergripande pivottabellen och kan formateras på olika sätt.

Hur man ställer in automatiskt format typ

Microsoft Excel erbjuder ett antal förinställda rapportformat. Aspose.Cells for Node.js via C++ stöder även dessa formateringsalternativ. För att komma åt dem:

  1. Ställ in PivotTable.setIsAutoFormat(value) till true.
  2. Tilldela ett formateringsalternativ från uppräkningen 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");

Hur man ställer in formateringsalternativ

Kodprovet nedan visar hur man formaterar pivottabellen för att visa totaler för rader och kolumner, och hur man ställer in rapportens fältsortering. Det visar också hur man ställer in en anpassad sträng för nollvärden.

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

Manuell formatering av utseende

För att manuellt formatera hur pivottabelrapporten ser ut, istället för att använda förinställda rapportformat, använd PivotTable.formatAll(style)- och PivotTable.format(row, column, style)-metoderna. Skapa en stilobjekt för önskad formatering, till exempel:

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

Hur man ställer in pivottabellsfältformatalternativ

Klassen PivotField representerar ett fält i en pivottabell och kan formateras på olika sätt. Kodprovet nedan visar hur man:

  • Tillgång till radfält.
  • Inställning av delsummer.
  • Inställning av autosortering.
  • Inställning av autoshow.

Hur man ställer in rad/kolumn/sida fältformat

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

Hur man ställer in datafältformat

Kodprovet nedan visar hur man ställer in visningsformat och nummerformat för datafält.

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

Hur man rensar pivottabellsfält

Klassen PivotFieldCollection har en metod som heter clear() som låter dig rensa pivottabellsfält. Använd den när du vill rensa alla pivottabellfält i områdena, till exempel sida, kolumn, rad eller data. Kodprovet nedan visar hur man rensar alla pivottabellfält i ett dataområde.

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