2016-10-13 72 views
2

*下面編輯與新代碼的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); 
+0

您是否嘗試過使用['PivotTables.Add' ](https://msdn.microsoft.com/en-us/library/office/ff196910.aspx)而不是使用嚮導? – PetLahev

回答

1

我剛纔已經回答了類似的問題有一天...

https://stackoverflow.com/a/39968355/1278553

簡而言之,在C#創建一個數據透視表的方法是:

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); 

我剽竊我先前的答案很重。

- 編輯二○一六年十月十七日 -

每更新的代碼,現在應該努力創造透視表:

var pch = oBook.PivotCaches(); 

Excel.Range pivotData = osheet.Range["A1", "B3"]; 

Excel.PivotCache pc = pch.Create(Excel.XlPivotTableSourceType.xlDatabase, pivotData); 
Excel.PivotTable pvt = pc.CreatePivotTable(osheet.Range["A12"], "MyPivotTable"); 

pvt.PivotFields("Name").Orientation = Excel.XlPivotFieldOrientation.xlRowField; 
pvt.AddDataField(pvt.PivotFields("Salary"), "Total Salary", 
    Excel.XlConsolidationFunction.xlSum); 
+0

感謝您的回覆@Hambone;我在答案底部的編輯代碼中錯過了顯而易見的東西嗎?數據透視表不會被創建。 – HShhanick

+1

@ HShhanick - 我根據您發佈的代碼更新瞭解決方案。唯一真正的錯誤是你需要用他們的名字來引用這些列。我測試了這個,並且它在我的最後工作。希望它也會在你的身上 – Hambone