Question

I'm writing a C# program that opens and read an Excel document and created a pivot table with some fields. One field is added oriented as Rows. The rest are calculated datafields. Everything is working and the data is added fine. There is only one problem. The calculated datafields are also added as rows, but I would like to have them as columns. In Excel, doing that manually means dragging the Values from Rows to Columns. But, how do I achieve the same programatically?

My code:

 Excel.PivotTable pivot1 = xlSheet.PivotTableWizard(
                 Excel.XlPivotTableSourceType.xlDatabase, // source type
                 pivotRange, // source
                 xlSheet.get_Range(Constants.PLACE_PIVOT_1), //destination
                 "Grade", // table name 
                 false,
                 false,
                 true,
                 false,
                 missing,
                 missing,
                 false,
                 false,
                 Excel.XlOrder.xlDownThenOver,
                 missing,
                 missing,
                 missing);

            Excel.PivotField betygField =
                (Excel.PivotField)pivot1.PivotFields("Grade type");
            betygField.Orientation =
                Excel.XlPivotFieldOrientation.xlRowField;
            betygField.Position = 1;

            // int dataIndex = 2;
            for (int f = Constants.START_START_SAMMAN; f < columns; f++) //add some array of columns as DataFields
            {
                string name = Convert.ToString(xlSheet.Cells[Constants.BEGIN_STAT_SAMMAN_Y, f].Value);

                Excel.PivotField f1 = pivot1.AddDataField((Excel.PivotField)pivot1.PivotFields(name),
                    String.Format("Sume of {0}", name), Excel.XlConsolidationFunction.xlSum);
                Excel.PivotField f2 = pivot1.AddDataField((Excel.PivotField)pivot1.PivotFields(name),
                    String.Format("Avg. of {0}", name), Excel.XlConsolidationFunction.xlAverage);

                f1.Orientation =
                Excel.XlPivotFieldOrientation.xlDataField;
                f1.NumberFormat = "0.00";

                f2.Orientation =
                Excel.XlPivotFieldOrientation.xlDataField;
                f2.NumberFormat = "0.00";
            }

            pivot1.GrandTotalName = "Totalt";
            pivot1.RowGrand = true;
            pivot1.ColumnGrand = true;
            pivot1.TableStyle2 = "PivotStyleLight16";
Was it helpful?

Solution

You may already know this trick...

When you know how to do something directly in Excel and you want to achieve the same programatically, you can make a good usage of the macro recorder (in developer tools, see first screenshot). After that, you have to translate the VBA macros into C# using interop Excel but if you know a little bit of VB that should not be difficult.

How to find the macro recorder

Then for your question I recorded the action to drag and drop a column field to the rows section. Thanks to the macro recorded it seems that you can achieve that by picking up the targeted field object and change its properties Orientation and Position.

The drag and drop action

The recorded macro

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top