I am trying to create pivot tables using data from CSV files. Here is an example:
Expand|Select|Wrap|Line Numbers
- String conStr = "Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=C:\\";
- String query = "SELECT * FROM content0.csv";
- Recordset content = new Recordset();
- Connection cn = new Connection();
- cn.Open(conStr, "", "", 0);
- content.Open(query, cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockOptimistic , 0);
- Application xlApp = new Application();
- Workbook xlWb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
- xlApp.Visible = true;
- Worksheet xlWs = (Worksheet) xlWb.Sheets.get_Item(1);
- PivotCaches xlPivCaches = xlApp.ActiveWorkbook.PivotCaches();
- PivotCache xlPc = xlPivCaches.Add(XlPivotTableSourceType.xlExternal, Missing.Value);
- xlPc.Recordset = content;
- PivotTable xlPt = xlPc.CreatePivotTable(xlWs.Cells[1, 1], "test", Missing.Value, Missing.Value);
- content.Close();
- cn.Close();
Exception from HRESULT: 0x800A03EC
at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName, BindingFla
gs flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.PivotCache.set_Reco rdset(Object )
at Project1.Sandbox.Main() in C:\Documents and Settings\vvishakh\My Documents
\Visual Studio 2005\Projects\Project1\Project1\Sandbox.cs:line 43
The above is caused by the line
Expand|Select|Wrap|Line Numbers
- xlPc.Recordset = content;
Expand|Select|Wrap|Line Numbers
- cn.CursorLocation = CursorLocationEnum.adUseClient;
I have been grappling with the problem over the past few days and would be very grateful if someone could tell me how to get Excel pivot caches to accept Recordsets using adUseServer.