我正在寫一個C#程序,它打開並讀取一個Excel文檔,並創建一個包含一些字段的數據透視表。一個字段被添加爲面向行。其餘的是計算的數據字段。一切正常,數據添加正常。只有一個問題。計算的數據字段也作爲行添加,但我希望將它們作爲列。在Excel中,手動操作意味着將值從行拖到列。但是,我如何實現相同的編程?Excel互操作和DataField方向
我的代碼:
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";
你怎麼mannage來解決這個問題?你能澄清一下嗎? 提前謝謝! –