How to do I change a Series color in Excel using C#?

20,333

Solution 1

Here is an example. I noticed when I tried to pass an integer that the bytes seem to be read in reverse order. So assigning 0xFF0000 makes the color blue and 0x0000FF turns the line red. Fortunately Microsoft provided an enumeration.

Random random = new Random();
Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = true;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);

Worksheet ws = (Worksheet)xla.ActiveSheet;

// Now create the chart.
ChartObjects chartObjs = (ChartObjects)ws.ChartObjects();
ChartObject chartObj = chartObjs.Add(150, 20, 300, 300);
Chart xlChart = chartObj.Chart;
for (int row = 0; row < 16; row++)
{
    ws.Cells[row + 2, 2] = row + 1;
    ws.Cells[row + 2, 3] = random.Next(100);
}

Range xValues = ws.Range["B2", "B17"];
Range values = ws.Range["C2", "C17"];

xlChart.ChartType = XlChartType.xlLine;
SeriesCollection seriesCollection = chartObj.Chart.SeriesCollection();

Series series1 = seriesCollection.NewSeries();
series1.XValues = xValues;
series1.Values = values;

series1.Format.Line.ForeColor.RGB = (int)XlRgbColor.rgbRed;
series1.Format.Line.Weight = 5;

Solution 2

Most of these type of problems come from not being able to find the exact object and property that needs to be changed.

A sure way to get to this information is to open your Excel file and go to the line chart. Start recording a macro, then change the item that you want to change. Stop recording the macro, and look at the code it generated. This will give you the exact object and property that must be used.

You can then make sure that your C# code is using the correct object and property syntax.

Solution 3

To change the color of a line series, you can use the border property:

series.Border.Color = (int)Excel.XlRgbColor.rgbGreen;

The colors can also be changed via the chart legend.

To change the color of a line:

((Excel.LegendEntry)chart.Legend.LegendEntries(1)).LegendKey.Border.ColorIndex = 10;

To change the color of a bar:

((Excel.LegendEntry)chart.Legend.LegendEntries(1)).LegendKey.Interior.Color = (int)Excel.XlRgbColor.rgbRed;

Solution 4

Recording a macro is definitely not a "sure" way to find the answer. For me in Excel 2007 with my chart recording gives me nothing except a long list of "ActiveSheet.ChartObjects("Chart 1").Activate". None of the answers above (or elsewhere when I searched) worked for me; however, I was able to change line thickness and text and change the markers.

The problem I was having with changing color is that the color is defaulting to Automatic for me when I make a new chart or add a new series. In order to turn off this behavior apparently you need to set the Line.Visible property to msoTriStateMixed. If I change Visible back to msoTrue, then the lines go back to their original color, and the "Automatic" is checked under Format Data Series, Line Style properties again. This code works for me in Excel 2007:

Excel.Series series = (Excel.Series)chartPage.SeriesCollection(1);
series.Format.Line.Weight = 1.0F;
series.Format.Line.Visible = MsoTriState.msoTriStateMixed;  //Tri-State 
series.Format.Line.ForeColor.RGB =(int)Microsoft.Office.Interop.Excel.XlRgbColor.rgbRed;
Share:
20,333
David Basarab
Author by

David Basarab

David Basarab Software craftsman that is constantly scrubbing my code.

Updated on August 27, 2020

Comments

  • David Basarab
    David Basarab over 3 years

    I have written a program in C# whereby it automatically generates a graph for me from a CSV file and puts it onto a new XLS file. However, I need to change the color of the Line (as it is a Line Chart) to red rather than the default blue.

    I am finding this extremely difficult to do and the stuff I've found online has not worked. Please can someone tell me how to do this?

  • Stewbob
    Stewbob about 13 years
    Yeah, for some strange reason, Excel has always used BGR to specify colors rather than RGB. That caused me no end of grief when I first started doing VBA in Excel. :)
  • trailblazer
    trailblazer over 10 years
    hey... the same happens for me, but in case of bar graph. The color changes to automatic, although I am not adding any new series. I just modify the range of the chart at runtime. Any idea how to fix that? I tried using myChartSeries.Format.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTriStateMixed; but it doesn't work. TIA.
  • pxm
    pxm about 10 years
    (+1) Great Help & for changing the exact color of the series I used: series1.Format.Fill.ForeColor.RGB=(int)XlRgbColor.xlDarkGree‌​n;