ピボット テーブルの書式設定
ピボット テーブルの外観
ピボット テーブルの作成方法では、単純なピボット テーブルの作成方法について説明しています。この記事では、さまざまなプロパティを設定してピボット テーブルの外観をカスタマイズする方法について説明します。
- ピボット テーブル形式のオプション
- ピボット フィールドのフォーマット オプション
- データ フィールド形式のオプション
ピボット テーブル形式オプションの設定
のピボットテーブルクラスはピボット テーブル全体を制御し、さまざまな方法でフォーマットできます。
オートフォーマットの種類の設定
Microsoft Excel には、あらかじめ設定されたさまざまなレポート形式が用意されています。 Aspose.Cells もこれらの書式設定オプションをサポートしています。それらにアクセスするには:
- セットするPivotTable.IsAutoFormatに真実.
- から書式設定オプションを割り当てます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"); | |
フォーマット オプションの設定
次のコード サンプルは、ピボット テーブルをフォーマットして行と列の総計を表示する方法と、レポートのフィールドの順序を設定する方法を示しています。また、null 値の顧客文字列を設定する方法も示します。
// 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"); | |
ルック アンド フィールを手動でフォーマットする
事前設定されたレポート形式を使用する代わりに、ピボット テーブル レポートの外観を手動で書式設定するには、ピボットテーブル.Format()とピボットテーブル.FormatAll()メソッド。目的のフォーマットのスタイル オブジェクトを作成します。次に例を示します。
// 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"); | |
ピボット フィールド形式オプションの設定
のピボットフィールドクラスはピボット テーブルのフィールドを表し、さまざまな方法でフォーマットできます。以下のコード サンプルは、次の方法を示しています。
- 行フィールドにアクセスします。
- 小計の設定。
- オートソートの設定。
- 自動表示の設定。
行/列/ページ フィールド フォーマットの設定
// 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"); | |
設定データフィールドのフォーマット
次のコード サンプルは、データ フィールドの表示形式と数値形式を設定する方法を示しています。
// 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"); | |
ピボット フィールドのクリア
のピボットフィールド コレクションという名前のメソッドがありますクリア()これにより、ピボット フィールドをクリアできます。ページ、列、行、データなど、領域内のすべてのピボット フィールドをクリアする場合に使用します。 以下のコード サンプルは、データ領域のすべてのピボット フィールドをクリアする方法を示しています。
// 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"); | |