Office.Interop.Excel从列表创建数据透视表

时间:2019-05-21 20:37:39

标签: c# excel office-interop

我正在尝试使用数据透视表生成Excel,我发现许多示例可以从范围或查询中设置PivotCache,也可以从列表中设置源<>

我尝试使用XlPivotTableSourceType.xlExternal,但返回类型错误的错误

        string FileTest = @"C:\Windows\Temp\123.xlsx";
        if (File.Exists(FileTest))
        {
            File.Delete(FileTest);
        }
        Microsoft.Office.Interop.Excel.Application oApp;
        Microsoft.Office.Interop.Excel.Workbook oBook;
        Microsoft.Office.Interop.Excel.Worksheet oSheet;

        oApp = new Microsoft.Office.Interop.Excel.Application();
        oBook = oApp.Workbooks.Add();
        oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.get_Item(1);

        var list = new List<Invoice>();
        list.Add(new Invoice { Article = "Art1", Customer = "Cus1", Price = 10 });
        list.Add(new Invoice { Article = "Art1", Customer = "Cus2", Price = 20 });
        list.Add(new Invoice { Article = "Art1", Customer = "Cus2", Price = 250 });
        list.Add(new Invoice { Article = "Art2", Customer = "Cus1", Price = 10 });
        list.Add(new Invoice { Article = "Art2", Customer = "Cus2", Price = 20 });
        list.Add(new Invoice { Article = "Art3", Customer = "Cus2", Price = 250 });

        oSheet = oApp.Worksheets[1];

        oSheet.Name = "Pivot Table";
        Microsoft.Office.Interop.Excel.Range oRange2 = oSheet.Cells[10, 10];

        Microsoft.Office.Interop.Excel.PivotCache oPivotCache = (Microsoft.Office.Interop.Excel.PivotCache)oBook.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlExternal, list);
        Microsoft.Office.Interop.Excel.PivotTable oPivotTable = (Microsoft.Office.Interop.Excel.PivotTable)oSheet.PivotTables().Add(PivotCache: oPivotCache, TableDestination: oRange2, TableName: "Summary");

        Microsoft.Office.Interop.Excel.PivotField oPivotField = ((Microsoft.Office.Interop.Excel.PivotField)oPivotTable.PivotFields("Article"));
        oPivotField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField;
        oPivotField.set_Subtotals(1, false);

        oPivotField = (Microsoft.Office.Interop.Excel.PivotField)oPivotTable.PivotFields("Customer");
        oPivotField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlDataField;

        oPivotField = (Microsoft.Office.Interop.Excel.PivotField)oPivotTable.PivotFields("Price");
        oPivotField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlDataField;

        oBook.SaveAs(FileTest);
        oBook.Close();
        oApp.Quit();

1 个答案:

答案 0 :(得分:0)

解决了从数据集中加载

        var sheetData = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
        sheetData.Name = "Data";


        object[,] arr = new object[dataTable.Rows.Count + 1, dataTable.Columns.Count];
        for (int c = 0; c < dataTable.Columns.Count; c++)
        {
            arr[0, c] = dataTable.Columns[c].ColumnName;
        }
        int arrRow = 1;
        for (int r = 0; r < dataTable.Rows.Count; r++)
        {
            DataRow dr = dataTable.Rows[r];
            for (int c = 0; c < dataTable.Columns.Count; c++)
            {
                arr[arrRow, c] = dr[c];
            }
            ++arrRow;
        }

        Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)sheetData.Cells[1, 1];
        Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)sheetData.Cells[2 + dataTable.Rows.Count - 1, dataTable.Columns.Count];
        Microsoft.Office.Interop.Excel.Range range = sheetData.get_Range(c1, c2);
        range.Value = arr;