Formatieren der Pivot Tabelle

Aussehen der Pivot-Tabelle

Wie man eine Pivot-Tabelle erstellt, erklärt, wie man eine einfache Pivot-Tabelle erstellt. Dieser Artikel beschreibt, wie man das Aussehen einer Pivot-Tabelle anpasst, indem man verschiedene Eigenschaften einstellt:

  • Optionen zum Formatieren von Pivot-Tabellen
  • Optionen zum Formatieren von Pivot-Feldern
  • Optionen zum Formatieren von Datenfeldern

Wie man die Formatierungsoptionen für Pivot-Tabellen festlegt

Die Klasse PivotTable steuert die gesamte Pivot-Tabelle und kann auf verschiedene Arten formatiert werden.

Wie man den AutoFormat-Typ festlegt

Microsoft Excel bietet eine Reihe von verschiedenen voreingestellten Berichtsformaten. Aspose.Cells für Python via .NET unterstützt diese Formatierungsoptionen ebenfalls. Um darauf zuzugreifen:

  1. Setzen Sie PivotTable.is_auto_format auf true.
  2. Weisen Sie eine Formatierungsoption aus der PivotTableAutoFormatType-Aufzählung zu.
from aspose.cells import Workbook
from aspose.cells.pivot import PivotTableAutoFormatType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
pivotindex = 0
# Get the first worksheet
worksheet = workbook.worksheets[0]
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report is automatically formatted
pivotTable.is_auto_format = True
# Setting the PivotTable atuoformat type.
pivotTable.auto_format_type = PivotTableAutoFormatType.REPORT5
# Saving the Excel file
workbook.save(dataDir + "output.xls")

Wie man Formatierungsoptionen festlegt

Das unten stehende Codebeispiel zeigt, wie die Pivot-Tabelle formatiert wird, um Gesamtsummen für Zeilen und Spalten anzuzeigen, und wie die Feldreihenfolge des Berichts festgelegt wird. Es zeigt auch, wie eine benutzerdefinierte Zeichenfolge für Nullwerte festgelegt wird.

from aspose.cells import PrintOrderType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report shows grand totals for rows.
pivotTable.row_grand = True
# Setting the PivotTable report shows grand totals for columns.
pivotTable.column_grand = True
# Setting the PivotTable report displays a custom string in cells that contain null values.
pivotTable.display_null_string = True
pivotTable.null_string = "null"
# Setting the PivotTable report's layout
pivotTable.page_field_order = PrintOrderType.DOWN_THEN_OVER
# Saving the Excel file
workbook.save(dataDir + "output.xls")

Manuelles Anpassen von Aussehen und Anmutung

Um das Erscheinungsbild des Pivot-Tabellenberichts manuell anzupassen, anstatt voreingestellte Berichtsformate zu verwenden, verwenden Sie die Methoden PivotTable.format_all(style) und PivotTable.format(row, column, style). Erstellen Sie ein Style-Objekt für Ihre gewünschte Formatierung, zum Beispiel:

from aspose.cells import BackgroundType, Workbook
from aspose.cells.pivot import PivotTableStyleType
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivot = workbook.worksheets[0].pivot_tables[0]
pivot.pivot_table_style_type = PivotTableStyleType.PIVOT_TABLE_STYLE_DARK1
style = workbook.create_style()
style.font.name = "Arial Black"
style.foreground_color = Color.yellow
style.pattern = BackgroundType.SOLID
pivot.format_all(style)
# Saving the Excel file
workbook.save(dataDir + "output.xls")

So setzen Sie Pivot-Feldformatoptionen

Die Klasse PivotField stellt ein Feld in einer Pivot-Tabelle dar und kann auf verschiedene Arten formatiert werden. Das unten stehende Codebeispiel zeigt, wie:

  • Auf Zeilenfelder zugreifen.
  • Untergesamtsummen einstellen.
  • Autosortierung einstellen.
  • Autoshow einstellen.

So setzen Sie Zeilen-/Spalten-/Seitenfeldformatoptionen

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldSubtotalType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report shows grand totals for rows.
pivotTable.row_grand = True
# Accessing the row fields.
pivotFields = pivotTable.row_fields
# Accessing the first row field in the row fields.
pivotField = pivotFields[0]
# Setting Subtotals.
pivotField.set_subtotals(PivotFieldSubtotalType.SUM, True)
pivotField.set_subtotals(PivotFieldSubtotalType.COUNT, True)
# Setting autosort options.
# Setting the field auto sort.
pivotField.is_auto_sort = True
# Setting the field auto sort ascend.
pivotField.is_ascend_sort = True
# Setting the field auto sort using the field itself.
pivotField.auto_sort_field = -5
# Setting autoShow options.
# Setting the field auto show.
pivotField.is_auto_show = True
# Setting the field auto show ascend.
pivotField.is_ascend_show = False
# Setting the auto show using field(data field).
pivotField.auto_show_field = 0
# Saving the Excel file
workbook.save(dataDir + "output.xls")

So setzen Sie Datenfeldformatoptionen

Das unten stehende Codebeispiel zeigt, wie man Anzeigeformate und Zahlenformat für Datenfelder einstellt.

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldDataDisplayFormat, PivotItemPosition
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Accessing the data fields.
pivotFields = pivotTable.data_fields
# Accessing the first data field in the data fields.
pivotField = pivotFields[0]
# Setting data display format
pivotField.data_display_format = PivotFieldDataDisplayFormat.PERCENTAGE_OF
# Setting the base field.
pivotField.base_field_index = 1
# Setting the base item.
pivotField.base_item_position = PivotItemPosition.NEXT
# Setting number format
pivotField.number = 10
# Saving the Excel file
workbook.save(dataDir + "output.xls")

So löschen Sie Pivot-Felder

Die Methode clear() der PivotFieldCollection-Klasse ermöglicht es Ihnen, Pivot-Felder zu löschen. Verwenden Sie sie, wenn Sie alle Pivot-Felder in den Bereichen wie Seite, Spalte, Zeile oder Daten löschen möchten. Das unten stehende Codebeispiel zeigt, wie man alle Pivot-Felder in einem Datenbereich löscht.

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
sheet = workbook.worksheets[0]
# Get the pivot tables in the sheet
pivotTables = sheet.pivot_tables
# Get the first PivotTable
pivotTable = pivotTables[0]
# Clear all the data fields
pivotTable.data_fields.clear()
# Add new data field
pivotTable.add_field_to_area(PivotFieldType.DATA, "Betrag Netto FW")
# Set the refresh data flag on
pivotTable.refresh_data_flag = False
# Refresh and calculate the pivot table data
pivotTable.refresh_data()
pivotTable.calculate_data()
# Saving the Excel file
workbook.save(dataDir + "output.xls")