limitation of rows number for making Pivot Table in c#

0

I am creating a PivotTable in excel by c# language. the program works till it comes to the number of rows which I use as a datasource for Pivot Table. when it is more than 65601(approximately) it gives an error like

Type Mismatch. (EXCEPTION from HResult 0x80020005 (DISP_E_TYPEMISMATCH))

When I make a pivot Table with less than 65601 (suppose 65501 rows which is tested) it works fine.

The excel version 15 (office 2013 installed). I searched and find this in wikipedia

Version 12.0 onwards, including the current Version 16.x, can handle over 1M (220 = 1048576) rows, and 16384 (214 as label 'XFD') columns.

So according to this line it should works which it didn't this is the code (which 100% works fine)

 excelApp = new Microsoft.Office.Interop.Excel.Application();
 excelWorkBook = excelApp.Workbooks.Open(currentDirectory);
 excelworksheet = excelWorkBook.ActiveSheet;
 sheet2 = excelWorkBook.Sheets.Add(); // Added new sheet to create Pivot Table
 string sheetSource = "Report" + reportDataDate;
((Microsoft.Office.Interop.Excel._Worksheet)excelworksheet).Activate();
 Microsoft.Office.Interop.Excel.Range oRange = excelworksheet.UsedRange;
 Microsoft.Office.Interop.Excel.PivotCache oPivotCache = (Microsoft.Office.Interop.Excel.PivotCache)excelWorkBook.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, oRange);  // Set the Source data range from First sheet

The last line is the line which the error appears. can anybody help me with the limitation of rows number for making Pivot Table?

Thank you

c#
excel
visual-studio
pivot-table
asked on Stack Overflow Aug 31, 2020 by nnmmss • edited Aug 31, 2020 by Panagiotis Kanavos

1 Answer

0

There is nothing wrong with my test, you could refer to my code to try it.

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excelApp = new Excel.Application();
var excelWorkBook = excelApp.Workbooks.Open(@"path\xx.xlsx");
Excel.Worksheet worksheet = excelWorkBook.Worksheets[1];
Excel.Worksheet sheet2 = excelWorkBook.Sheets.Add(); // Added new sheet to create Pivot Table
sheet2.Name = "Pivot Table"; // Assigned sheet Name
worksheet.Activate();
((Excel._Worksheet)worksheet).Activate();
Excel.Range oRange = worksheet.UsedRange;
Excel.PivotCaches pch = excelWorkBook.PivotCaches();
pch.Add(Excel.XlPivotTableSourceType.xlDatabase, oRange).CreatePivotTable(sheet2.Range["A1"], "PivTbl_1", Type.Missing, Type.Missing);// Create Pivot table
Excel.PivotTable pvt = sheet2.PivotTables("PivTbl_1") as Excel.PivotTable;  // Set the Source data range from First sheet
Excel.PivotField fld = ((Excel.PivotField)pvt.PivotFields("Price"));// Create a Pivot Field in Pivot table
fld.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
fld.set_Subtotals(1, false);
excelWorkBook.Save();
excelApp.Quit();
answered on Stack Overflow Sep 2, 2020 by Arya Ding - MSFT

User contributions licensed under CC BY-SA 3.0