I've been able to drag a PivotTable out onto the dance floor, but haven't been able to get it to boogie down - it seems we keep stepping on each other's toes. I've got data already on the sheet from which to build the Pivot Table. This class describes that data:
public class PriceVarianceData
{
public String Unit { get; set; }
public String ShortName { get; set; }
public String ItemCode { get; set; }
public String Description { get; set; }
public String PriceWeek { get; set; }
public String Week { get; set; }
public String Price { get; set; }
public String Variance { get; set; }
public String VarianceAverage { get; set; }
public int RegionOrder { get; set; }
public int ContractPrice { get; set; }
}
The first column ("A") on the sheet contains the Unit values, the second column ("B") displays the ShortName values, etc.
I am using the following code to create a PivotTable from this data:
private void AddPivotTable()
{
pivotData = _xlSheet.Range["A1:K1600"];
pivotDestination = _xlSheet.Range["A1602", useDefault];
_xlBook.PivotTableWizard(
Excel.XlPivotTableSourceType.xlDatabase,
pivotData,
pivotDestination,
pivotTableName,
true,
true,
true,
true,
useDefault,
useDefault,
false,
false,
Excel.XlOrder.xlDownThenOver,
0,
useDefault,
useDefault
);
// Set variables used to manipulate the Pivot Table.
pivotTable = (Excel.PivotTable)_xlSheet.PivotTables(pivotTableName);
shortnamePivotField = (Excel.PivotField)pivotTable.PivotFields(2);
itemcodePivotField = (Excel.PivotField)pivotTable.PivotFields(3);
descriptionPivotField = (Excel.PivotField)pivotTable.PivotFields(4);
priceweekPivotField = (Excel.PivotField)pivotTable.PivotFields(5);
weekPivotField = (Excel.PivotField)pivotTable.PivotFields(6);
regionorderPivotField = (Excel.PivotField)pivotTable.PivotFields(10);
// Format the Pivot Table.
pivotTable.Format(Excel.XlPivotFormatType.xlReport2);
pivotTable.InGridDropZones = false;
pivotTable.SmallGrid = false;
pivotTable.ShowTableStyleRowStripes = true;
pivotTable.TableStyle2 = "PivotStyleLight1";
// The PivotFields have the following interesting properties:
// Orientation, Function, Calculation
// Page Field
//shortnamePivotField.Orientation = Excel.XlPivotFieldOrientation.xlPageField;
// Column Field
priceweekPivotField.Orientation = Excel.XlPivotFieldOrientation.xlColumnField;
// Row Fields
descriptionPivotField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
//regionorderPivotField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
// Data Fields
//contractPricePivotField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;
//contractPricePivotField.Function = Excel.XlConsolidationFunction.xlSum;
itemcodePivotField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;
itemcodePivotField.Function = Excel.XlConsolidationFunction.xlSum;
}
Admittedly, the hardcoding of the range values is a bit doofusesque, but this is an experimental project which will always return the same (amount of) data, so it works in this case with this somewhat contrived case.
The code above does add a PivotTable to the sheet, but it provides the viewer scant value, as it makes little sense (the last few rows of raw data are shown at the top; the PivotTable begins on row 1602):
I realize the problem exists because I'm not designating the correct PivotFields as xlPageField, xlColumnField[s], xlRowField[s], and xlDataField[s]. The problem is one of groklessness on my part; instead of wrapping my mind around this data, my grey matter is lying flaccid at an angle of repose along the ramparts of the PivotTable.
What do I need to do in order to alter the PivotTable's appearance so that it shows:
Totals for each ItemCode (such as "110051") regardless of ShortName.
...and perhaps in another PivotTable:
Totals for each ShortName (such as "T&T") regardless of ItemCode.
???
UPDATE
Inspired by a comment here, I added column headings, which changed the PivotTable a bit:
UPDATE 2
Here is a fakely-generated (with the GUI, not in code) PivotTable that I want to create in code:
What code is needed to specify "SHORT NAME" as the "Report Filter", so that it will generate a drop-down as shown above with the various values of ShortName selectable?
UPDATE 3
This was my best guess:
// Page Field
shortnamePivotField.Orientation = Excel.XlPivotFieldOrientation.xlPageField;
// Row Fields
descriptionPivotField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
itemcodePivotField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
// Data Fields
pricePivotField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;
pricePivotField.Function = Excel.XlConsolidationFunction.xlSum;
...but was no better than throwing code at the wall, since it didn't work: