以Odata作为源代码,在Excel工作簿中以编程方式创build数据透视表

我正在尝试使用C#在Excel工作簿中创build数据透视表。 来源是Odata供稿url。 我写了下面的代码:

string connectionString = @"DATAFEED;Data Source=" + odataURL + ";Namespaces to Include=*;Max Received Message Size=4398046511104;Integrated Security=Basic;User ID=" + "xyz" + ";Password=" + "bvby" + ";Persist Security Info=true;Base Url=" + odataURL; Excel.PivotCache pivotCache = app.ActiveWorkbook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlExternal, Type.Missing); pivotCache.Connection = connectionString; 

当我做pivotCache.Connection时抛出下面的exception:

 System.Runtime.InteropServices.COMException was caught HResult=-2146827284 Message=Exception from HRESULT: 0x800A03EC Source="" ErrorCode=-2146827284 

堆栈跟踪:

 at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.PivotCache.set_Connection(Object value) at ConsoleApplication2.Program.createWorkBookHelper(Application app, Workbook workbook, String odataURL, String nameOfWorkBook) in d:\ReportingApp\Dev\PDS\Excel Plugin\ConsoleApplication2\ConsoleApplication2\Program.cs:line 36`