*下面編輯與新代碼的Excel VSTO + C#.NET +創建數據透視表
我試圖通過創建C#.NET中透視表的Excel VSTO但保持越來越異常拋出各國:
Exception from HRESULT: 0x800A03EC
片我試圖創建樞軸表稱爲oSheet和相同具有源數據
創建用於樞軸表源的範圍內的片材:
Excel.Range pivotData = osheet.Range["A1", "B3"];
甲範圍,其中所述樞轉表將是:
Excel.Range pivotDest = osheet.Range["A12", "B14"];
然後嘗試與該創建該表:
osheet.PivotTableWizard(
Excel.XlPivotTableSourceType.xlDatabase,
pivotData,
pivotDest,
"Table",
true,
true,
true,
true,
true,
true,
false,
false,
Excel.XlOrder.xlDownThenOver,
0,
false,
false
);
該錯誤是在表嚮導線中發生。
任何想法是什麼導致錯誤或通過C#.Net創建數據透視表的更好方法?
*新代碼
Excel.Application oApp;
Excel.Worksheet osheet;
Excel.Workbook oBook;
oApp = new Excel.Application();
oBook = oApp.Workbooks.Add();
osheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
osheet.Cells[1, 1] = "Name";
osheet.Cells[1, 2] = "Salary";
osheet.Cells[2, 1] = "Frank";
osheet.Cells[2, 2] = 1500000;
osheet.Cells[3, 1] = "Millford";
osheet.Cells[3, 2] = 2200;
//Now capture range of first sheet = I will need this to create pivot table
Excel.Range oRange = osheet.Range["A1", "B3"];
var pch = oBook.PivotCaches();
Excel.Range pivotData = osheet.Range["A1", "B3"];
Excel.PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, pivotData);
Excel.PivotTable pvt = pc.CreatePivotTable(osheet.Range["A12"], "MyPivotTable");
pvt.PivotFields("Col1").Orientation = XlPivotFieldOrientation.xlRowField;
pvt.PivotFields("Col2").Orientation = XlPivotFieldOrientation.xlColumnField;
pvt.AddDataField(pvt.PivotFields("Col3"), "Sum of Col3", XlConsolidationFunction.xlSum);
您是否嘗試過使用['PivotTables.Add' ](https://msdn.microsoft.com/en-us/library/office/ff196910.aspx)而不是使用嚮導? – PetLahev