c# - 更改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();
推荐阅读
- javascript - 将三元运算符转换为 if
- c# - 是否可以使用 IEnumerable 为类中的字段创建一个 foreach 循环?
- python - 如何通过 Rstudio 在 python 脚本中正确导入熊猫
- php - SQL 突发问题 - 一般错误:1364 字段没有默认值
- javascript - 根据其他两个输入更改文本字段的输入值
- java - 调用另一个类时递归不起作用
- vb.net - 无法从 Windows 7 电脑使用 Visual Basic 发送电子邮件:“操作已超时”
- javascript - 你能将 React hook const 命名为与已定义的 const 相同的名称吗?
- php - 如何检查条件是否在php中定义为变量?
- java - 使用 java.lang.ProcessBuilder 运行 root 命令