简体   繁体   English

如何使用C#在Excel中更改系列颜色?

[英]How to do I change a Series color in Excel using C#?

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. 我用C#编写了一个程序,它从CSV文件中自动生成一个图形并将其放到一个新的XLS文件中。 However, I need to change the color of the Line (as it is a Line Chart) to red rather than the default blue. 但是,我需要将Line的颜色(因为它是折线图)更改为红色而不是默认的蓝色。

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? 请有人能告诉我怎么做吗?

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. 因此,分配0xFF0000会使颜色变为蓝色,而0x0000FF会将该线变为红色。 Fortunately Microsoft provided an enumeration. 幸运的是,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;

Most of these type of problems come from not being able to find the exact object and property that needs to be changed. 大多数这类问题来自无法找到需要更改的确切对象和属性。

A sure way to get to this information is to open your Excel file and go to the line chart. 获取此信息的可靠方法是打开Excel文件并转到折线图。 Start recording a macro, then change the item that you want to change. 开始录制宏,然后更改要更改的项目。 Stop recording the macro, and look at the code it generated. 停止录制宏,并查看它生成的代码。 This will give you the exact object and property that must be used. 这将为您提供必须使用的确切对象和属性。

You can then make sure that your C# code is using the correct object and property syntax. 然后,您可以确保您的C#代码使用正确的对象和属性语法。

To change the color of a line series, you can use the border property: 要更改线系列的颜色,可以使用border属性:

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

The colors can also be changed via the chart legend. 也可以通过图表图例更改颜色。

To change the color of a line: 要更改线条的颜色:

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

To change the color of a bar: 要更改条形的颜色:

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

Recording a macro is definitely not a "sure" way to find the answer. 录制宏绝对不是找到答案的“确定”方式。 For me in Excel 2007 with my chart recording gives me nothing except a long list of "ActiveSheet.ChartObjects("Chart 1").Activate". 对于我在Excel 2007中使用我的图表记录除了一长串“ActiveSheet.ChartObjects(”Chart 1“)之外什么都没有给我。激活”。 None of the answers above (or elsewhere when I searched) worked for me; 上面的答案(或我搜索的其他地方)都没有为我工作; however, I was able to change line thickness and text and change the markers. 但是,我能够改变线条粗细和文字并更改标记。

The problem I was having with changing color is that the color is defaulting to Automatic for me when I make a new chart or add a new series. 我改变颜色的问题是,当我制作新图表或添加新系列时,颜色默认为自动。 In order to turn off this behavior apparently you need to set the Line.Visible property to msoTriStateMixed. 为了关闭此行为,显然您需要将Line.Visible属性设置为msoTriStateMixed。 If I change Visible back to msoTrue, then the lines go back to their original color, and the "Automatic" is checked under Format Data Series, Line Style properties again. 如果我将Visible更改回msoTrue,则线条将恢复为原始颜色,并再次在“格式数据系列”,“线条样式”属性下选中“自动”。 This code works for me in Excel 2007: 此代码适用于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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#更改Excel数据标签的背景颜色 - How to change the background color of excel data label using C# 如何使用 C# 动态更改 Excel 单元格颜色 - How to Change Excel Cell Color Dynamically using C# 如何将C#中的颜色动态更改为十六进制值? - How do I dynamically change color in C# to a hex value? c#如何更改特定文本框的颜色为空? - c# How do i change the color of a specific textbox if it is empty? 如何在c#中更改笔的颜色? - How do I change the color of the pen in c#? 更改同系列C#的颜色列 - change the color column of the same series C# 如何使用C#在剪贴板中设置文本,以便将每个文本粘贴到具有不同背景/字体颜色的excel中? - How do I set a text in clipboard using C# in order to past it into excel with different background/font color for each cell? 在Excel中使用C#中的条件格式更改单元格颜色 - Cell color change In Excel Using Conditional formatting in C# 如何使用C#获取Excel中图表中系列总数的计数? - How can I get a count of the total number of series in a chart in excel using c#? 如何使用C#在Excel中的图表上添加多个系列 - How to add multiple series on a Chart in Excel using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM