通过C#在Excel中创建数据透视图

时间:2019-01-28 10:59:56

标签: c# excel pivot

我编写了用于在Excel中创建数据透视图的C#代码。我无法按预期创建数据透视图。我想用不同的颜色区分我的两个数据字段(MD和MDC)。通过我的代码,我得到了两种相同的颜色。这是我的示例代码

    private void button1_Click(object sender, EventArgs e)
    {
        string FileTest = @"C:\Users\nshebann\Desktop\WindowsFormsApp1\123.xlsx";
        if (File.Exists(FileTest))
        {
            File.Delete(FileTest);
        }
        Excel.Application oApp;
        Excel.Workbook oBook;
        Excel.Worksheet oSheet;

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

        oSheet.Cells[1, 1] = "WW";
        oSheet.Cells[1, 2] = "MD";
        oSheet.Cells[1, 3] = "MDC"; 

        oSheet.Cells[2, 1] = "ww1";
        oSheet.Cells[2, 2] = "4";
        oSheet.Cells[2, 3] = "5";

        oSheet.Cells[3, 1] = "ww2";
        oSheet.Cells[3, 2] = "18";
        oSheet.Cells[3, 3] = "4";

        oSheet.Cells[4, 1] = "ww3";
        oSheet.Cells[4, 2] = "10";
        oSheet.Cells[4, 3] = "4";

        oSheet.Cells[5, 1] = "ww4";
        oSheet.Cells[5, 2] = "16";
        oSheet.Cells[5, 3] = "4";

        oSheet.Cells[6, 1] = "ww16";
        oSheet.Cells[6, 2] = "1";
        oSheet.Cells[6, 3] = "4";

        oSheet.Cells[7, 1] = "ww27";
        oSheet.Cells[7, 2] = "1";
        oSheet.Cells[7, 3] = "4";

        oSheet.Cells[8, 1] = "ww32";
        oSheet.Cells[8, 2] = "9";
        oSheet.Cells[8, 3] = "4";

        oSheet.Cells[9, 1] = "ww34";
        oSheet.Cells[9, 2] = "3";
        oSheet.Cells[9, 3] = "4";

        Excel.Range oRange = oSheet.Range["A1", "C9"];
        oSheet = oApp.Worksheets[1];

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

        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 oPivotField = ((Excel.PivotField)oPivotTable.PivotFields("WW"));
        oPivotField.Orientation = Excel.XlPivotFieldOrientation.xlRowField;
        oPivotField.set_Subtotals(1, false);

        oPivotField = (Excel.PivotField)oPivotTable.PivotFields("MD");
        oPivotField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;

        oPivotField = (Excel.PivotField)oPivotTable.PivotFields("MDC");
        oPivotField.Orientation = Excel.XlPivotFieldOrientation.xlDataField;

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

我得到的输出如下。

Present Output 我用黄色突出显示了值。我想在图例部分中找到那个。

但是我想得到如下输出 enter image description here

如何通过代码将“轴”部分中存在的值移动到“图例”部分?

1 个答案:

答案 0 :(得分:0)

我认为这将为您带来追随感:

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

Excel.PivotCache pc = oBook.PivotCaches().Create(Excel.XlPivotTableSourceType.xlDatabase, oRange);
Excel.PivotTable oPivotTable = pc.CreatePivotTable(oRange2, "Summary");

Excel.Shape chart = oSheet.Shapes.AddChart(Excel.XlChartType.xl3DColumnClustered,
    100, 300, 500, 500);
chart.Chart.SetSourceData(oPivotTable.TableRange1);

oPivotTable.PivotFields("WW").Orientation = Excel.XlPivotFieldOrientation.xlRowField;

oPivotTable.AddDataField(oPivotTable.PivotFields("MD"),
    "Sum of MD", Excel.XlConsolidationFunction.xlSum);
oPivotTable.AddDataField(oPivotTable.PivotFields("MDC"), 
    "Sum of MDC", Excel.XlConsolidationFunction.xlSum);