I have a pre-made Excel with PivotTable that gets Data from another worksheet, I fill via c# code.
In the PivotTable there are three Row fields, which after getting populated and the Pivot refreshed via code, make all the rows expanded after opening the Excel.
I already tried using the DrilledDown
method (in the fields, but I get an error), the ShowDetails
only works in the items (inside the fields).
I got it to work, but it takes too much time because it uses the ShowDetails
in each item, is there any other way, and quick for collapsing the fields? (opening the Excel and doing it manually is not an option).
My Code (which collapses the fields are):
listItems.Add("Data");
listItems.Add("Months");
listItems.Add("Grocery");
Microsoft.Office.Interop.Excel.PivotFields pfs = (Microsoft.Office.Interop.Excel.PivotFields)pivot.PivotFields();
foreach (String s in listItems)
{
Microsoft.Office.Interop.Excel.PivotField pf = (Microsoft.Office.Interop.Excel.PivotField)pivot.PivotFields(s);
foreach (Microsoft.Office.Interop.Excel.PivotItem item in (Microsoft.Office.Interop.Excel.PivotItems)pf.PivotItems())
{
if (item.Value == "(blank)")
item.Visible = false;
item.ShowDetail = false;
}
}
The thing is, maybe there's an easy and quicker way? I was trying something like
pf.DrilledDown = False;
or
pf.ShowDetail = False;
But it doesn't work. Any Ideas?