根据数据透视表的PivotField显示名称获取单元格对象
Contents
[
Hide
]
Aspose.Cells for Python via .NET提供了PivotTable.get_cell_by_display_name(display_name)方法,您可以使用它来访问数据透视表的显示字段的单元格对象。当您想要突出显示或格式化数据透视表的字段标题时,这种方法非常有用。本文解释了如何通过显示数据字段的名称检索单元格对象,然后对其应用格式。
如何通过数据透视表的PivotField的DisplayName获取Cell对象
以下代码访问工作表的第一个透视表,然后通过透视表的第二个数据字段的显示名称获取单元格。然后将单元格的填充颜色和字体颜色分别更改为浅蓝色和黑色。下面的屏幕截图显示了代码执行之前和之后透视表的外观。
透视表 - 在之前 |
---|
![]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import Workbook | |
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(".") | |
# Create workbook object from source excel file | |
workbook = Workbook(dataDir + "source.xlsx") | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Access first pivot table inside the worksheet | |
pivotTable = worksheet.pivot_tables[0] | |
# Access cell by display name of 2nd data field of the pivot table | |
cell = pivotTable.get_cell_by_display_name(pivotTable.data_fields[0].display_name) | |
# Access cell style and set its fill color and font color | |
style = cell.get_style() | |
style.foreground_color = Color.light_blue | |
style.font.color = Color.black | |
# Set the style of the cell | |
pivotTable.format(cell.row, cell.column, style) | |
# Save workbook | |
workbook.save(dataDir + "output_out.xlsx") |
透视表 - 在之后 |
---|
![]() |