简体   繁体   中英

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

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?

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;

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.

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM