How to change series color in Excel using C #?

I wrote a C # program in which it automatically generates a graph for me from a CSV file and puts it in a new XLS file. However, I need to change the color of the line (since this is a line chart) to red, and not the default.

I find it extremely complex, and the material I found on the Internet did not work. Please can someone tell me how to do this?

+3
source share
4 answers

Most of these problems arise from the inability to find the exact object and property that needs to be changed.

- Excel . , , . , . , .

, # .

+6

. , , , . 0xFF0000 , 0x0000FF - . , Microsoft .

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;
+6

, border:

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

.

:

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

:

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

"" . Excel 2007 , "ActiveSheet.ChartObjects(" 1 "). ". ( - , ) ; .

, , , , . , Line.Visible msoTriStateMixed. Visible msoTrue, , "" " ", " ". 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;
+1

Source: https://habr.com/ru/post/1716779/


All Articles