Change color of series in excel line chart

Viewed 335

I am trying to iterate through series in a line chart and change the color of the series. Below is my code, and it currently only works for bar charts:

//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++;
}

To change the color of line charts in VBA you have to target the points of the series. I assume the same thing applies to C#. However, I have not figured out how to do this. When I run the above code on line charts it simply changes nothing.

Thank you very much in advance.

1 Answers

You can try the following code to Change color of series in excel line chart.

        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();
Related