title: c# Collapse Fields in Excel PivotTable
body: <p>I have a pre-made Excel with PivotTable that gets Data from another worksheet, I fill via c# code.</p>
  <p>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.</p>
  <p>I already tried using the <code>DrilledDown</code> method (in the fields, but I get an error), the <code>ShowDetails</code> only works in the items (inside the fields).</p>
  <p>I got it to work, but it takes too much time because it uses the <code>ShowDetails</code> 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).</p>
  <p>My Code (which collapses the fields are):</p>
  <pre class="lang-cs prettyprint-override"><code>listItems.Add(&quot;Data&quot;);
  listItems.Add(&quot;Months&quot;);
  listItems.Add(&quot;Grocery&quot;);
      
  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 == &quot;(blank)&quot;)
              item.Visible = false;
  
          item.ShowDetail = false;
      }  
  }
  
  </code></pre>
  <p>The thing is, maybe there's an easy and quicker way? I was trying something like</p>
  <pre><code>pf.DrilledDown = False;
  </code></pre>
  <p>or</p>
  <pre><code>pf.ShowDetail = False;
  </code></pre>
  <p>But it doesn't work. Any Ideas?</p>