Pivot Table a Worksheet Excel C# Visual Studio 2010 RRS feed

  • Question

  • Hi everyone!

    I'm trying to generate a Pivot Table from a Excel file using C# with Visual Studio 2010. I have seen a lot of examples but I can't run anyone...

    This is my code:

            Workbooks libro = Excel.Workbooks;
            Workbook libroActual = libro.Add();
            Sheets sheets = libroActual.Worksheets;
            Microsoft.Office.Interop.Excel._Worksheet Worksheet = libroActual.ActiveSheet;
            Worksheet.Name = "Desglose";
            // Operations here with sheed 1
            Worksheet sheetMMPP = sheets.Add();
            sheetMMPP.Name = "MMPP";
            //Range of the origin sheet
            Microsoft.Office.Interop.Excel.Range oRange = Worksheet.UsedRange;
            Microsoft.Office.Interop.Excel.Range oRange2 = sheetMMPP.Cells[1, 1];
            Microsoft.Office.Interop.Excel.PivotCache oPivotCache = (Microsoft.Office.Interop.Excel.PivotCache)libroActual.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, oRange2);
            Microsoft.Office.Interop.Excel.PivotCaches pch = libroActual.PivotCaches();
            Microsoft.Office.Interop.Excel.PivotTable pvt = sheetMMPP.PivotTables("PivTbl_1") as Microsoft.Office.Interop.Excel.PivotTable;

    What could am I doing wrong? Is there some example to create a Pivot Table using an existing excel sheet?

    Thank you in advance!

    Monday, November 25, 2019 8:27 AM

All replies