Nil
Nil

Reputation: 411

Visual Studio Excel Pivot Table not showing data

I am writing an application to add a pivot table sheet in an existing excel workbook. Here's the code:

Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        if (e.Name.StartsWith("~$"))
            return;

        Excel.Application oApp;
        Excel.Worksheet oSheet;
        Excel.Workbook oBook;


        oApp = new Excel.Application();
        try
        {
            oBook = oApp.Workbooks.Open(e.FullPath);
            oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
            //Excel.Range finalCell = oSheet.Cells[2, oSheet.Rows.Count];
            Excel.Range oRange = oSheet.get_Range("A1", "M9");

            if (oApp.Application.Sheets.Count < 2)
            {
                oSheet = (Excel.Worksheet)oBook.Worksheets.Add();
            }
            else
            {
                oSheet = oApp.Worksheets[2];
            }
            oSheet.Name = "Pivot Table";
            // specify first cell for pivot table
            Excel.Range oRange2 = oSheet.Cells[3,1];

            // create Pivot Cache and Pivot Table
            Excel.PivotCache oPivotCache = (Excel.PivotCache)oBook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlDatabase, oRange);
            Excel.PivotTable oPivotTable = (Excel.PivotTable)oSheet.PivotTables().Add(PivotCache: oPivotCache, TableDestination: oRange2, TableName: "Summary");


            Excel.PivotField oPivotField1 = (Excel.PivotField)oPivotTable.PivotFields("Field1");
            oPivotField1.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            oPivotField1.Position = 1;
            oPivotField1.Name = "Field1";

            Excel.PivotField oPivotField2 = (Excel.PivotField)oPivotTable.PivotFields("Field2");
            oPivotField2.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
            oPivotField2.Position = 2;
            oPivotField2.Name = "Field2";

            oBook.Save();
            Console.WriteLine("Operation Complete");
}

This is inside a FileSystemWatcher's event handler for file creation. The program runs and creates the sheet for the Pivot table, but data values are not listed and also i get two separate columns. Here's the screen

Please let me know what i'm doing wrong. Using Visual Studio 2015 and project type is Windows Form Application.

Upvotes: 0

Views: 547

Answers (1)

MacroMarc
MacroMarc

Reputation: 3324

Replace the pivot cache/table creation lines:

Excel.PivotCache oPivotCache = (Excel.PivotCache)oBook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlDatabase, oRange);
Excel.PivotTable oPivotTable = (Excel.PivotTable)oSheet.PivotTables().Add(PivotCache: oPivotCache, TableDestination: oRange2, TableName: "Summary");

with

PivotCache oPivotCache = oBook.PivotCaches().Create(XlPivotTableSourceType.xlDatabase, oRange, XlPivotTableVersionList.xlPivotTableVersion14);
PivotTable oPivotTable = oPivotCache.CreatePivotTable(TableDestination: oRange2, TableName: "Summary");

Note that you are doing a lot of redundant casting. You generally don't have to cast all those pivotCache and pivotfield variables.

Upvotes: 1

Related Questions