首页 > 解决方案 > 更改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#。但是,我还没有弄清楚如何做到这一点。当我在折线图上运行上面的代码时,它根本没有改变。

非常感谢您提前。

标签: c#excelvisual-studio

解决方案


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

推荐阅读