I am creating a Pivot Table programmatically in c#. the code fines when the Source data ( which is a sheet in excel). It works fine till the source sheets contains 65536 rows, more than that, an error appears which says
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
This is the Code:
Microsoft.Office.Interop.Excel.Application excelApp = null;
Microsoft.Office.Interop.Excel.Workbook excelWorkBook = null;
Microsoft.Office.Interop.Excel.Worksheet excelworksheet = null;
Microsoft.Office.Interop.Excel.Worksheet sheet2 = null;
string currentDirectory = @"d:\Temp\ds1.xlsx";
int pivotPosition = 6;
excelApp = new Microsoft.Office.Interop.Excel.Application();
excelWorkBook = excelApp.Workbooks.Open(currentDirectory);
excelworksheet = excelWorkBook.ActiveSheet;
excelApp.Visible = true;
Microsoft.Office.Interop.Excel.Range oRange = excelworksheet.UsedRange;
Microsoft.Office.Interop.Excel.PivotCaches pch = excelWorkBook.PivotCaches();
sheet2 = excelWorkBook.Sheets.Add();
pch.Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, oRange).CreatePivotTable(sheet2.Cells[pivotPosition, 1], "PivTbl_1", Type.Missing, Type.Missing);// Create Pivot table
Microsoft.Office.Interop.Excel.PivotTable pvt = sheet2.PivotTables("PivTbl_1") as Microsoft.Office.Interop.Excel.PivotTable;
Microsoft.Office.Interop.Excel.PivotField fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("ptime")); // Create a Pivot Field in Pivot table
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField; // Add the pivot field as Row Field
fld.Caption = "Hour";
int HourMaxMinCountItem = fld.VisibleItems.Count;
fld.set_Subtotals(1, false); //Remove Subtotals for each row and column
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("RankingSub"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("Substation"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("RankingVol"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("Voltage"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("RankingTit"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("ColumnTitle"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("RankingS6"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("S6_NAME"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
fld.set_Subtotals(1, false);
fld = ((Microsoft.Office.Interop.Excel.PivotField)pvt.PivotFields("ValueAmount"));
fld.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlDataField;
pvt.ColumnGrand = false;
pvt.RowGrand = false;
but the number of rows in Source sheet is variable, could be more that 100,000 rows, how can I handle that?
Excel 2003 you had a limit of 65535 rows, check the version of the interop libraries you are using.
The name of the library for 2003 is "Microsoft Excel 12.0 Object Library", the name for 2010 is "Microsoft Excel 15.0 Object Library".
If you change your references to 15+ you should be able to use up to a million ish rows.
Interop libraries use the COM (Component Object Model) interface, the Excel application will need to be installed on the dev machine and the machine the application will be running on. You can't distribute the interops as part of your application, they need to be available on the machines GAC
User contributions licensed under CC BY-SA 3.0