[英]How can I determine how many rows a PivotTable generates (Aspose Cells)?
I need to conditionally colorize ranges in a PivotTable. 我需要有条件地着色数据透视表中的范围。 I tried to do it this way:
我试图这样做:
private void ColorizeContractItemBlocks(List<string> contractItemDescs)
{
int FIRST_DESCRIPTION_ROW = 7;
int DESCRIPTION_COL = 1;
int ROWS_BETWEEN_DESCRIPTIONS = 4;
int rowsUsed = pivotTableSheet.Cells.Rows.Count;
int currentRowBeingExamined = FIRST_DESCRIPTION_ROW;
// Loop through PivotTable data, colorizing contract items
while (currentRowBeingExamined < rowsUsed)
{
Cell descriptionCell = pivotTableSheet.Cells[currentRowBeingExamined, DESCRIPTION_COL];
String desc = descriptionCell.Value.ToString();
if (contractItemDescs.Contains(desc))
{
// args are firstRow, firstColumn, totalRows, totalColumns
Range rangeToColorize = pivotTableSheet.Cells.CreateRange(
currentRowBeingExamined, 0,
ROWS_BETWEEN_DESCRIPTIONS, _grandTotalsColumnPivotTable + 1);
Style style = workBook.Styles[workBook.Styles.Add()];
style.BackgroundColor = CONTRACT_ITEM_COLOR;
StyleFlag styleFlag = new StyleFlag();
styleFlag.All = true;
rangeToColorize.ApplyStyle(style, styleFlag);
}
currentRowBeingExamined = currentRowBeingExamined + ROWS_BETWEEN_DESCRIPTIONS;
}
}
...but it doesn't work, because rowsUsed does not take into consideration the rows on the PivotTable on the pivotTableSheet, and so my while loop is never entered. ...但是它不起作用,因为rowsUsed没有考虑到pivotTableSheet上数据透视表上的行,因此从不输入while循环。
How can I determine how many rows the PivotTable takes up on the sheet, so that I can loop through the PivotTable? 如何确定数据透视表在工作表上占据多少行,以便可以遍历数据透视表?
Or, am I approaching this the wrong way? 还是我以错误的方式来处理? Is there a different standard way of manipulating the styles/formatting of a PivotTable after it has been generated?
数据透视表生成后,是否有其他不同的标准方式来处理数据透视表的样式/格式?
The RowRange
property of the pivot table should take you row by row through every element in the table: 数据透视表的
RowRange
属性应使您逐行浏览表中的每个元素:
Excel.Worksheet ws = wb.Sheets["Sheet1"];
Excel.PivotTable pt = ws.PivotTables("PivotTable1");
Excel.Range cell;
foreach (Excel.Range row in pt.RowRange)
{
cell = ws.Cells[row.Row, 5]; // for example, whatever is in column E
// do your formatting here
}
There are other ranges available -- for example, I typically only care about: 还有其他可用范围-例如,我通常只关心:
pt.DataBodyRange
Which is every cell within the actual pivot table (whatever is being aggregated). 哪个是实际数据透视表中的每个单元(无论正在汇总什么)。
@B. @B。 Clay Shannon, You may consider using any of the following APIs for your requirement.
克莱·香农(Clay Shannon),您可以考虑使用以下任何API来满足您的需求。 I have added comments to the code for your reference.
我已经在代码中添加了注释,供您参考。
var book = new Workbook(dir + "sample.xlsx");
var sheet = book.Worksheets[0];
var pivot = sheet.PivotTables[0];
// DataBodyRange returns CellArea that represents range between the header row & insert row
var dataBodyRange = pivot.DataBodyRange;
Console.WriteLine(dataBodyRange);
// TableRange1 returns complete Pivot Table area except page fields
var tableRange1 = pivot.TableRange1;
Console.WriteLine(tableRange1);
// TableRange2 returns complete Pivot Table area including page fields
var tableRange2 = pivot.TableRange2;
Console.WriteLine(tableRange2);
// ColumnRange returns range that represents the column area of the Pivot Table
var columnRange = pivot.ColumnRange;
Console.WriteLine(columnRange);
// RowRange returns range that represents the row area of the Pivot Table
var rowRange = pivot.RowRange;
Console.WriteLine(rowRange);
In case you still face any difficulty, please share your sample spreadsheet along with desired results (that you may create manually in Excel application) in a thread at Aspose.Cells support forum for thorough investigation. 如果您仍然遇到任何困难,请在Aspose.Cells支持论坛的一个线程中共享示例电子表格以及所需的结果(可以在Excel应用程序中手动创建),以进行深入研究。
Note : I am working as Developer Evangelist at Aspose. 注意 : 我在Aspose担任开发人员推广人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.