怎么办我使用C#改变一个颜色系列在Excel中? [英] How to do I change a Series color in Excel using C#?

查看:558
本文介绍了怎么办我使用C#改变一个颜色系列在Excel中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C#编写的程序,由此它会自动从CSV文件生成一个图表,我,并把它到一个新的XLS文件。但是,我需要改变线路的颜色(因为它是一个折线图),以红色,而不是默认的蓝色。

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?

推荐答案

下面是一个例子。我注意到当我试图传递一个整数,字节似乎以相反的顺序读取。因此,分配为0xFF0000 使颜色蓝色和 0x0000FF 变为行红色。幸运的是微软提供的一个枚举

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;

这篇关于怎么办我使用C#改变一个颜色系列在Excel中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆