Dar formato a la tabla dinámica
Apariencia de la tabla dinámica
Cómo crear una tabla dinámica explica cómo crear una tabla dinámica simple. Este artículo describe cómo personalizar la apariencia de una tabla dinámica estableciendo diversas propiedades:
- Opciones de formato de tabla dinámica
- Opciones de formato de campos de tabla dinámica
- Opciones de formato de campos de datos
Cómo establecer opciones de formato de tabla dinámica
La clase PivotTable controla la tabla dinámica general y se puede formatear de varias maneras.
Cómo establecer el tipo de autoformato
Microsoft Excel ofrece varios formatos predefinidos de informes. Aspose.Cells for Node.js via C++ soporta estas opciones de formato también. Para acceder a ellos:
- Establezca PivotTable.setIsAutoFormat(value) en verdadero.
- Asignar una opción de formato de la enumeración 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"); |
Cómo establecer opciones de formato
El ejemplo de código a continuación muestra cómo dar formato a la tabla dinámica para mostrar totales generales para filas y columnas, y cómo establecer el orden de los campos del informe. También muestra cómo establecer una cadena personalizada para los valores nulos.
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"); |
Dar formato manualmente al aspecto visual
Para formatear cómo se ve el informe de tabla dinámica manualmente, en lugar de usar formatos de informe preestablecidos, use los métodos PivotTable.formatAll(style) y PivotTable.format(row, column, style). Cree un objeto de estilo para su formato deseado, por ejemplo:
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"); |
Cómo establecer opciones de formato de campo de tabla dinámica
La clase PivotField representa un campo en una tabla dinámica y se puede formatear de varias formas. El ejemplo de código a continuación muestra cómo:
- Acceder a los campos de fila.
- Establecer subtotales.
- Establecer orden automático.
- Establecer autover.
Cómo establecer el formato de campos de fila/columna/página.
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"); |
Cómo establecer el formato de campos de datos.
El ejemplo de código a continuación muestra cómo establecer formatos de visualización y formato numérico para los campos de datos.
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"); |
Cómo limpiar los campos de filtro.
La clase PivotFieldCollection tiene un método llamado clear() que te permite borrar campos de tabla dinámica. Úsalo cuando quieras borrar todos los campos de tabla dinámica en las áreas, por ejemplo, página, columna, fila o datos. El ejemplo de código a continuación muestra cómo borrar todos los campos de tabla dinámica en un área de datos.
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"); |