Based on code I found here, I\'m trying to create a Pivot Table like so:
var range = _xlSheet.Range[_xlSheet.Cells[6, 1],
_xlSheet
Without seeing the specifics of your workbook, it's hard to diagnose some of the issues you list. The basic means of generating a pivot table would be this:
Range sourceData = _xlBook.Worksheets["Produce Usage by Month"].Range["A6:F94"];
PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData);
PivotTable pvt = pc.CreatePivotTable(sheet2.Range["A1"], "SummaryPivot");
pvt.PivotFields("Col1").Orientation = XlPivotFieldOrientation.xlRowField;
pvt.PivotFields("Col2").Orientation = XlPivotFieldOrientation.xlColumnField;
pvt.AddDataField(pvt.PivotFields("Col3"), "Sum of Col3", XlConsolidationFunction.xlSum);
It looks like your code is not being really explicit about some of the objects. For example Produce Usage by Month!A6:F94
means something to Excel (and is probably interpreted by VBA), but I think the interop libraries need more explicit declaration of objects. In strong typed, this would be what I listed for the sourceData
object. sheet2
is therefore an Excel.Worksheet
object.
Regarding some of the warnings, such as overwrite warnings, if you change the DisplayAlerts
property of the Excel application object to false, it will take care of many of these.