2017-08-15 89 views
0

如何在我的Excel饼图上创建自定义颜色? 我的饼图有5个切片。以下是我的源代码。c#Excel饼图自定义颜色

using Excel = Microsoft.Office.Interop.Excel; 
Excel.Range chartRange; 

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); 
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 500, 350); 
Excel.Chart chartPage = myChart.Chart; 

chartRange = xlWorkSheet.get_Range("A3", "B7");    
chartPage.ChartStyle = 209; 
chartPage.HasTitle = true; 
chartPage.ChartTitle.Text = "HeaderText Title"; 
chartPage.SetSourceData(chartRange, misValue);    
chartPage.ChartType = Excel.XlChartType.xl3DPieExploded; 
chartPage.Elevation = 35; 
chartPage.ApplyDataLabels(Excel.XlDataLabelsType.xlDataLabelsShowLabelAndPercent ,false, true, true, false, true, false, true, true, Separator:System.Environment.NewLine); 

xlWorkBook.SaveAs(saveAsLocation); 
xlWorkBook.Close(true, misValue, misValue); 
xlApp.Quit(); 
+0

有人可以给你的最佳建议不一定是鱼,但如何将记录的宏转换为C#来为生活寻找答案:https://blogs.msdn.microsoft.com/csharpfaq/2010/09/27 /转换为a-vba-macro-to-c-4-0/ –

回答

1

你可以改变你的饼图的每一点这样的ColorIndex

var series = (Excel.SeriesCollection)chartPage.SeriesCollection(); 
series.Item(1).Points(1).Interior.ColorIndex = 3; //Red 
series.Item(1).Points(2).Interior.ColorIndex = 4; //Green 
series.Item(1).Points(3).Interior.ColorIndex = 5; //Blue 
series.Item(1).Points(4).Interior.ColorIndex = 6; //Yellow 
series.Item(1).Points(5).Interior.ColorIndex = 7; //Magenta 

这里是可用的颜色的完整列表msdn

ColorIndex属性可以有之间的有效整数参数0和56生成颜色。

0

我相信你可以改变ForeColor财产的系列点格式。例如:

var series = (Excel.SeriesCollection)chartPage.SeriesCollection(); 
var points = (Excel.Points)series.Item(1).Points(); 
points.Item(1).Format.Fill.ForeColor.RGB = (int)Excel.XlRgbColor.rgbRed; 
points.Item(2).Format.Fill.ForeColor.RGB = (int)Excel.XlRgbColor.rgbBlue; 

...等等。

+0

只需小心在Interop中投放双点。这里是[posts](https://stackoverflow.com/questions/29067714)的[couple](https://stackoverflow.com/questions/13069153)。 –