此示例演示如何创建自定义样式并将其应用于数据透视表。默认情况下,工作簿的数据透视表样式集合 (TableStyleCollection) 包含类似于 Microsoft® Excel® 的内置样式和 None 样式,后者指定不应将任何格式应用于数据透视表。
数据透视表样式由 TableStyle 对象定义,该对象由表样式元素 (TableStyle.TableStyleElements) 的集合组成。每个表格样式元素 (TableStyleElement) 指定数据透视表特定元素的格式设置。TableStyleElementType 枚举器列出支持的表样式元素。 使用 TableStyleElement 对象的属性可以自定义数据透视表的相应元素的边框 (TableStyleElement.Borders)、填充 (TableStyleElement.Fill) 和字体 (TableStyleElement.Font)。
要创建自定义数据透视表样式,请执行以下操作。
下面的示例复制了内置的数据透视表样式,并通过更改整个表、列标题和总计行的格式特征来修改新样式。
Vb.Net |
'按数据透视表在集合中的名称访问数据透视表。 Dim pivotTable As DevExpress.Spreadsheet.PivotTable = worksheet.PivotTables("PivotTable1") '获取要复制的数据透视表样式。 Dim sourceStyle As DevExpress.Spreadsheet.TableStyle = workbook.TableStyles(DevExpress.Spreadsheet.BuiltInPivotStyleId.PivotStyleMedium3) '复制数据透视表样式。 Dim customStyle As DevExpress.Spreadsheet.TableStyle = sourceStyle.Duplicate() '修改所创建的数据透视表样式所需的格式特征。 customStyle.BeginUpdate() Try '指定列标题的格式特征。 Dim header As DevExpress.Spreadsheet.TableStyleElement = customStyle.TableStyleElements(DevExpress.Spreadsheet.TableStyleElementType.HeaderRow) header.Fill.BackgroundColor = Color.FromArgb(&H1F, &H3E, &H7E) '指定整个表的格式特征。 Dim wholeTable As DevExpress.Spreadsheet.TableStyleElement = customStyle.TableStyleElements(DevExpress.Spreadsheet.TableStyleElementType.WholeTable) wholeTable.Fill.BackgroundColor = Color.FromArgb(&HF1, &HF4, &HD0) wholeTable.Borders.RemoveBorders() '指定汇总行的格式特征。 Dim totalRowStyle As DevExpress.Spreadsheet.TableStyleElement = customStyle.TableStyleElements(DevExpress.Spreadsheet.TableStyleElementType.TotalRow) totalRowStyle.Fill.BackgroundColor = Color.FromArgb(166, 166, 166) totalRowStyle.Borders.RemoveBorders() Finally customStyle.EndUpdate() End Try '将创建的自定义样式应用于数据透视表。 pivotTable.Style = customStyle |
C# |
// 按数据透视表在集合中的名称访问数据透视表。 DevExpress.Spreadsheet.PivotTable pivotTable = worksheet.PivotTables["PivotTable1"]; // 获取要复制的数据透视表样式。 DevExpress.Spreadsheet.TableStyle sourceStyle = workbook.TableStyles(DevExpress.Spreadsheet.BuiltInPivotStyleId.PivotStyleMedium3); // 复制数据透视表样式。 DevExpress.Spreadsheet.TableStyle customStyle = sourceStyle.Duplicate(); // 修改所创建的数据透视表样式所需的格式特征。 customStyle.BeginUpdate(); try { // 指定列标题的格式特征。 DevExpress.Spreadsheet.TableStyleElement header = customStyle.TableStyleElements(DevExpress.Spreadsheet.TableStyleElementType.HeaderRow); header.Fill.BackgroundColor = Color.FromArgb(0x1F, 0x3E, 0x7E); // 指定整个表的格式特征。 DevExpress.Spreadsheet.TableStyleElement wholeTable = customStyle.TableStyleElements(DevExpress.Spreadsheet.TableStyleElementType.WholeTable); wholeTable.Fill.BackgroundColor = Color.FromArgb(0xF1, 0xF4, 0xD0); wholeTable.Borders.RemoveBorders(); // 指定汇总行的格式特征。 DevExpress.Spreadsheet.TableStyleElement totalRowStyle = customStyle.TableStyleElements(DevExpress.Spreadsheet.TableStyleElementType.TotalRow); totalRowStyle.Fill.BackgroundColor = Color.FromArgb(166, 166, 166); totalRowStyle.Borders.RemoveBorders(); } finally { customStyle.EndUpdate(); } // 将创建的自定义样式应用于数据透视表。 pivotTable.Style = customStyle; |
下图说明了应用自定义样式(工作簿在 Microsoft® Excel® 中打开)时的数据透视表外观。