Previous topicNext topic
Help > 开发指南 > Excel > API > 示例 > 数据透视表 >
如何:将自定义样式应用于数据透视表

此示例演示如何创建自定义样式并将其应用于数据透视表。默认情况下,工作簿的数据透视表样式集合 (TableStyleCollection) 包含类似于 Microsoft® Excel® 的内置样式和 None 样式,后者指定不应将任何格式应用于数据透视表。

数据透视表样式由 TableStyle 对象定义,该对象由表样式元素 (TableStyle.TableStyleElements) 的集合组成。每个表格样式元素 (TableStyleElement) 指定数据透视表特定元素的格式设置。TableStyleElementType 枚举器列出支持的表样式元素。 使用 TableStyleElement 对象的属性可以自定义数据透视表的相应元素的边框 (TableStyleElement.Borders)、填充 (TableStyleElement.Fill) 和字体 (TableStyleElement.Font)。

要创建自定义数据透视表样式,请执行以下操作。

  1. 通过调用 TableStyleCollection.Add 方法,向 IWorkbook.TableStyles 集合添加新的数据透视表样式。此方法返回 TableStyle 对象,该对象表示新创建的数据透视表样式。
    请注意,您还可以基于现有数据透视表样式(例如,内置数据透视表样式)创建自定义数据透视表样式。为此,请使用 TableStyle.Duplicate 方法。此方法创建指定样式的副本,并返回表示所创建样式的 TableStyle 对象。
  2. 将 TableStyle.IsPivotStyle 属性设置为 true,将 TableStyle.IsTableStyle 属性设置为 false,以指示创建的样式应仅应用于数据透视表。对于基于内置数据透视表样式的自定义样式,可以跳过此步骤,因为此类样式会从内置样式中复制这些属性的相应值。
  3. 调用 TableStyle.BeginUpdate 方法。
  4. 通过相应的 TableStyleElementType 枚举成员从 TableStyle.TableStyleElements 集合中访问要修改的表样式元素。使用 TableStyleElement 属性指定元素所需的格式设置。如果需要从元素中删除现有格式,请使用其 TableStyleElement.Clear 方法。
    对要修改的所有表格样式元素重复此步骤。
  5. 调用 TableStyle.EndUpdate 方法。
  6. 使用 PivotTable.Style 属性将创建的样式应用于数据透视表。

下面的示例复制了内置的数据透视表样式,并通过更改整个表、列标题和总计行的格式特征来修改新样式。

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® 中打开)时的数据透视表外观。