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



我正在尝试在折线图中循环访问系列并更改系列的颜色。下面是我的代码,它目前仅适用于条形图:

//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#。但是,我还没有弄清楚如何做到这一点。当我在折线图上运行上面的代码时,它没有任何变化。

提前非常感谢你。

您可以尝试以下代码来更改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();

最新更新