在Excel折线图中更改系列的颜色

时间:2020-06-28 22:51:19

标签: c# excel visual-studio

我正在尝试遍历折线图中的系列并更改系列的颜色。以下是我的代码,目前仅适用于条形图:

//Series Colors (customSeriesColorsArray is the array containing my colors)

int i = 0;
foreach ( Excel.Series series in chartObject.Chart.FullSeriesCollection())
{
    series.Interior.Color = customSeriesColorsArray[i];                  
    i++;
}

要在VBA中更改折线图的颜色,必须确定系列的点。我假设同样的事情适用于C#。但是,我还没有弄清楚该怎么做。当我在折线图上运行以上代码时,它什么都没有改变。

非常感谢您。

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码来更改excel折线图中的系列颜色。

        Microsoft.Office.Interop.Excel.Application xla = new 
        Microsoft.Office.Interop.Excel.Application();
        Workbook wb = xla.Workbooks.Open("D:\\1.XLSX");
        Worksheet ws = (Worksheet)xla.ActiveSheet;
        ChartObject xlChartObject = ws.ChartObjects(1);
        // Get the chart from the chart object
        Chart xlChart = xlChartObject.Chart;
        XlRgbColor[] color={ XlRgbColor.rgbWhite, XlRgbColor.rgbBlue,XlRgbColor.rgbYellow,XlRgbColor.rgbRed,XlRgbColor.rgbPink};
        Microsoft.Office.Interop.Excel.FullSeriesCollection fullSeriesCollection = xlChart.FullSeriesCollection();
        int i = 0;
        Border line;
        foreach (Series xlSeries in fullSeriesCollection)
        {
            line = xlSeries.Border;
            line.Color = color[i];
       
            i++;
        }
        wb.Save();