使用Microsoft.Office.Interop.Excel C#如何添加系列?

时间:2018-08-29 05:21:33

标签: c# excel graph excel-interop

Combination Graph 我正在尝试使用C#在Excel中绘制一个组合图,如上所示。 我可以使用下面给出的代码绘制一个系列图。但是我在添加另一个系列时遇到问题。如何添加其他系列?

下面是我的代码:

Microsoft.Office.Interop.Excel.Application xlApp;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Microsoft.Office.Interop.Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        //add data 
        xlWorksheet.Cells[1, 1] = "Label";
        xlWorksheet.Cells[1, 2] = "Date Time";
        xlWorksheet.Cells[1, 3] = "Alarm Broadcaster";
        xlWorksheet.Cells[1, 4] = "Alarm ID";
        xlWorksheet.Cells[1, 5] = "Alarm Discription";
        xlWorksheet.Cells[1, 6] = "Occurence";
        xlWorksheet.Cells[1, 7] = "Cumuative Frequency";

        for (int i = 0; i < list_alarmObj.Count; i++)
        {
            xlWorksheet.Cells[i + 2, 1] = list_alarmObj[i].discription;
            xlWorksheet.Cells[i + 2, 2] = list_alarmObj[i].date;
            xlWorksheet.Cells[i + 2, 3] = list_alarmObj[i].broadcaster;
            xlWorksheet.Cells[i + 2, 4] = list_alarmObj[i].alarmid;
            xlWorksheet.Cells[i + 2, 5] = list_alarmObj[i].discription;
            xlWorksheet.Cells[i + 2, 6] = list_alarmObj[i].occurrence;
            xlWorksheet.Cells[i + 2, 7] = cumulativeFreq[i];
        }

        Range chartRange;
        ChartObjects xlCharts = (ChartObjects)xlWorksheet.ChartObjects(Type.Missing);
        ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
        Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart;
        string endrange = "f" + (list_alarmObj.Count + 1);
        chartRange = xlWorksheet.get_Range("f1", endrange);
        chartPage.SetSourceData(chartRange, misValue);
        chartPage.ChartType = XlChartType.xlColumnClustered;
        xlWorkBook.SaveAs("histogram-informations.xls", XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

0 个答案:

没有答案