Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys I am trying to make a pivot table programmtically but i getting this error :
C#
"Exception from HRESULT: 0x800A03EC"

on line :
C#
pivotCache.Connection = connection;


My code is as follows :

C#
Microsoft.Office.Interop.Excel.Application objApp;
       Microsoft.Office.Interop.Excel.Workbook objBook;
       Microsoft.Office.Interop.Excel.Sheets objSheets;
       Microsoft.Office.Interop.Excel.Workbooks objBooks;

       string connection = @"OLEDB;Provider=SQLOLEDB.1;Data Source=.;Initial Catalog=Employee;persist security info=False;user id=sa;pwd=server";
       string command = "SELECT * FROM employee";


       objApp = new Microsoft.Office.Interop.Excel.Application();

       objBooks = objApp.Workbooks;
       objBook = objApp.Workbooks.Add(Missing.Value);
       objSheets = objBook.Worksheets;

       Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)objSheets[1];
       sheet1.Name = "ACCOUNTS";
       Microsoft.Office.Interop.Excel.PivotCache pivotCache = objBook.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, sheet1.UsedRange);


       //Microsoft.Office.Interop.Excel.PivotCache pivotCache = this.Application.ActiveWorkbook.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlExternal, Missing.Value);

       pivotCache.Connection = connection;
       pivotCache.MaintainConnection = true;
       pivotCache.CommandText = command;
       pivotCache.CommandType = Microsoft.Office.Interop.Excel.XlCmdType.xlCmdSql;
       Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)objApp.ActiveSheet;
       Microsoft.Office.Interop.Excel.PivotTables pivotTables = (Microsoft.Office.Interop.Excel.PivotTables)sheet.PivotTables(Missing.Value);
       Microsoft.Office.Interop.Excel.PivotTable pivotTable = pivotTables.Add(pivotCache, objApp.ActiveCell, "PivotTable1", Missing.Value, Missing.Value);
       pivotTable.SmallGrid = false;
       pivotTable.ShowTableStyleRowStripes = true;
       pivotTable.TableStyle2 = "PivotStyleLight1";
       Microsoft.Office.Interop.Excel.PivotField pageField = (Microsoft.Office.Interop.Excel.PivotField)pivotTable.PivotFields("SalesTerritory");
       pageField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
       Microsoft.Office.Interop.Excel.PivotField rowField = (Microsoft.Office.Interop.Excel.PivotField)pivotTable.PivotFields("FullName");
       rowField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField;
       pivotTable.AddDataField(pivotTable.PivotFields("2004"), "Sum of 2004", Microsoft.Office.Interop.Excel.XlConsolidationFunction.xlSum);
Posted
Updated 3-Aug-11 21:24pm
v2

1 solution

No one can help in this ???
Anyways I am able to populate the pivot table now, i am working on generating pivot chart programmatically now.
I saw many peoples facing the same problem so I am posting my code :


C#
Microsoft.Office.Interop.Excel.Application objApp;
       Microsoft.Office.Interop.Excel.Workbook objBook;
       Microsoft.Office.Interop.Excel.Sheets objSheets;
       Microsoft.Office.Interop.Excel.Workbooks objBooks;

       objApp = new Microsoft.Office.Interop.Excel.Application();
       objBooks = objApp.Workbooks;
       objBook = objApp.Workbooks.Add(Missing.Value);
       objSheets = objBook.Worksheets;

       Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)objSheets[1];
       sheet1.Activate();

       string connection = @"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Data Source=.;Initial Catalog=Employee";
       string command = "SELECT * FROM employee";
       Microsoft.Office.Interop.Excel.PivotCache pivotCache = objBook.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlExternal, Missing.Value);
       pivotCache.Connection = connection;
       pivotCache.MaintainConnection = true;
       pivotCache.CommandText = command;
       pivotCache.CommandType = Microsoft.Office.Interop.Excel.XlCmdType.xlCmdSql;
       Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)objApp.ActiveSheet;
       Microsoft.Office.Interop.Excel.PivotTables pivotTables = (Microsoft.Office.Interop.Excel.PivotTables)sheet.PivotTables(Missing.Value);
       Microsoft.Office.Interop.Excel.PivotTable pivotTable = pivotTables.Add(pivotCache, objApp.ActiveCell, "PivotTable1",Missing.Value, Missing.Value);
       pivotTable.SmallGrid = false;
       pivotTable.ShowTableStyleRowStripes = true;
       pivotTable.TableStyle2 = "PivotStyleLight1";
       Microsoft.Office.Interop.Excel.PivotField pageField =  (Microsoft.Office.Interop.Excel.PivotField)pivotTable.PivotFields("UserName");
       pageField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
       Microsoft.Office.Interop.Excel.PivotField rowField =  (Microsoft.Office.Interop.Excel.PivotField)pivotTable.PivotFields("FirstName");
       rowField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField;
       pivotTable.AddDataField(pivotTable.PivotFields("LastName"), "ID", Microsoft.Office.Interop.Excel.XlConsolidationFunction.xlSum);


       ExcelSaveAs(objApp, objBook, "c:NewPivote.xls");

   }

   static string ExcelSaveAs(Microsoft.Office.Interop.Excel.Application objApp, Microsoft.Office.Interop.Excel.Workbook objBook, string path)
   {
       try
       {
           objApp.DisplayAlerts = false;
           objBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet, Missing.Value, Missing.Value, false, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
           objApp.DisplayAlerts = true;
           return null;
       }
       catch (Exception e)
       {
           StackTrace st = new StackTrace(new StackFrame(true));
           StackFrame sf = st.GetFrame(0);
           return (e.Message + "\n" + "Method" + sf.GetMethod().ToString() + "\n" + "Line" + sf.GetFileLineNumber().ToString());
       }
 
Share this answer
 
v3
Comments
Member 790656 19-Nov-12 3:51am    
You make my day!...The key thing yo point - to others - is the OLEDB word at the beggining of the connection string.

Even inside de Excel WorkBookConnection details, this is not appearing, which leads to a big confussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900