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
Impostazione delle opzioni di formato della tabella pivot
La classe PivotTable controlla complessivamente la tabella pivot e può essere formattata in vari modi.
Impostazione del tipo di formato automatico
Microsoft Excel offre diversi formati preimpostati per i report. Anche Aspose.Cells supporta queste opzioni di formattazione. Per accedervi:
- Imposta PivotTable.IsAutoFormat su vero.
- Assegna un’opzione di formattazione dall’enumerazione 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. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
int pivotindex = 0; | |
// Get the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing the PivotTable | |
PivotTable pivotTable = worksheet.PivotTables[pivotindex]; | |
// Setting the PivotTable report is automatically formatted | |
pivotTable.IsAutoFormat = true; | |
// Setting the PivotTable atuoformat type. | |
pivotTable.AutoFormatType = Aspose.Cells.Pivot.PivotTableAutoFormatType.Report5; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); | |
Impostazione delle Opzioni di Formato
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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
int pivotindex = 0; | |
// Accessing the PivotTable | |
PivotTable pivotTable = worksheet.PivotTables[pivotindex]; | |
// Setting the PivotTable report shows grand totals for rows. | |
pivotTable.RowGrand = true; | |
// Setting the PivotTable report shows grand totals for columns. | |
pivotTable.ColumnGrand = true; | |
// Setting the PivotTable report displays a custom string in cells that contain null values. | |
pivotTable.DisplayNullString = true; | |
pivotTable.NullString = "null"; | |
// Setting the PivotTable report's layout | |
pivotTable.PageFieldOrder = 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.Format() e PivotTable.FormatAll(). Creare un oggetto stile per la formattazione desiderata, ad esempio:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
var pivot = workbook.Worksheets[0].PivotTables[0]; | |
pivot.PivotTableStyleType = PivotTableStyleType.PivotTableStyleDark1; | |
Style style = workbook.CreateStyle(); | |
style.Font.Name = "Arial Black"; | |
style.ForegroundColor = Color.Yellow; | |
style.Pattern = BackgroundType.Solid; | |
pivot.FormatAll(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); | |
Impostazione delle 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.
Impostazione delle opzioni di formato campi riga/colonna/pagina
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
int pivotindex = 0; | |
// Accessing the PivotTable | |
PivotTable pivotTable = worksheet.PivotTables[pivotindex]; | |
// Setting the PivotTable report shows grand totals for rows. | |
pivotTable.RowGrand = true; | |
// Accessing the row fields. | |
Aspose.Cells.Pivot.PivotFieldCollection pivotFields = pivotTable.RowFields; | |
// Accessing the first row field in the row fields. | |
Aspose.Cells.Pivot.PivotField pivotField = pivotFields[0]; | |
// Setting Subtotals. | |
pivotField.SetSubtotals(Aspose.Cells.Pivot.PivotFieldSubtotalType.Sum, true); | |
pivotField.SetSubtotals(Aspose.Cells.Pivot.PivotFieldSubtotalType.Count, true); | |
// Setting autosort options. | |
// Setting the field auto sort. | |
pivotField.IsAutoSort = true; | |
// Setting the field auto sort ascend. | |
pivotField.IsAscendSort = true; | |
// Setting the field auto sort using the field itself. | |
pivotField.AutoSortField = -5; | |
// Setting autoShow options. | |
// Setting the field auto show. | |
pivotField.IsAutoShow = true; | |
// Setting the field auto show ascend. | |
pivotField.IsAscendShow = false; | |
// Setting the auto show using field(data field). | |
pivotField.AutoShowField = 0; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); | |
Impostazione del formato dei campi dati
Il campione di codice sottostante mostra come impostare i formati di visualizzazione e il formato numerico per i campi dati.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Get the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
int pivotindex = 0; | |
// Accessing the PivotTable | |
PivotTable pivotTable = worksheet.PivotTables[pivotindex]; | |
// Accessing the data fields. | |
Aspose.Cells.Pivot.PivotFieldCollection pivotFields = pivotTable.DataFields; | |
// Accessing the first data field in the data fields. | |
Aspose.Cells.Pivot.PivotField pivotField = pivotFields[0]; | |
// Setting data display format | |
pivotField.ShowValuesSetting.CalculationType = PivotFieldDataDisplayFormat.PercentageOf; | |
// Setting the base field. | |
pivotField.ShowValuesSetting.BaseFieldIndex = 1; | |
// Setting the base item. | |
pivotField.ShowValuesSetting.BaseItemPositionType = Aspose.Cells.Pivot.PivotItemPositionType.Next; | |
// Setting number format | |
pivotField.Number = 10; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); | |
Cancellazione dei Campi di Tabella 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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load a template file | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Get the first worksheet | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Get the pivot tables in the sheet | |
PivotTableCollection pivotTables = sheet.PivotTables; | |
// Get the first PivotTable | |
PivotTable pivotTable = pivotTables[0]; | |
// Clear all the data fields | |
pivotTable.DataFields.Clear(); | |
// Add new data field | |
pivotTable.AddFieldToArea(PivotFieldType.Data, "Betrag Netto FW"); | |
// Set the refresh data flag on | |
pivotTable.RefreshDataFlag = false; | |
// Refresh and calculate the pivot table data | |
pivotTable.RefreshData(); | |
pivotTable.CalculateData(); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); | |