【问题标题】:c# Excel Pie Chart Custom Colorc# Excel饼图自定义颜色
【发布时间】:2018-01-23 00:22:25
【问题描述】:

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

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();

【问题讨论】:

标签: c# excel charts


【解决方案1】:

我相信您可以更改系列点格式的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;

...等等。

【讨论】:

  • 请注意在 Interop 中宣传双点。这是posts 中的couple
【解决方案2】:

您可以像这样更改饼图每个点的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 之间的有效整数参数来生成颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多