2017-08-15 282 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)。 –