2016-03-07 83 views
1

是否有人知道如何设置C#中PowerPoint 2010饼图的切片/图例的颜色?我无法从MSDN网站上读取任何内容。我无法找到正确的方法来获得正确的对象和属性。设置饼图切片和图例的颜色

编辑︰ 嗯,我想添加代码,但我有什么不工作,所以我不知道它会有多大的帮助或信息。我找不到要使用哪个对象/方法/属性来访问饼图切片颜色。我已经尝试过使用Point,LegendKey,LegendEntry,Legend以及相关的方法/属性。我已经尝试了很多甚至没有在我的代码中代表的东西。

但是,对于它的价值,这是我的代码是什么现在:

PowerPoint.Series series = (PowerPoint.Series)xlChart.SeriesCollection(1); 
PowerPoint.Point point = (PowerPoint.Point)series.Points(xlCol); 

point.Interior.ColorIndex = Convert.ToInt32(dataArray[2, i]); 

PowerPoint.Legend legend = (PowerPoint.Legend)xlChart.Legend; 
PowerPoint.LegendEntry lentry = (PowerPoint.LegendEntry)legend.LegendEntries(1); 
+1

为我们提供了一些代码? –

+0

嗯,我可以告诉你一些代码,但它是错误的。我不确定这会有帮助。我甚至不认为我很亲密。我的意思是,我甚至无法从在线的MSND文档中找出哪些对象/方法/属性需要更改。我已经尝试过从SeriesCollection派生的Point。我从LegendEntry尝试了LegendKey的LegendKey,它是从LegendEntries派生而来的,衍生自Legend。我尝试了我现在无法记住的事情。Anway,对于它的价值,这就是我的代码目前的样子: – Henry

+0

假设你通过COM来完成这项工作并且PowerPoint文件已经存在是否公平? –

回答

2

Interior.ColorIndex将不起作用,因为在枚举中只有两个值:xlColorIndexAutomaticxlColorIndexNone

但是,你非常接近。你想要的是Interior.Color。我使用十六进制来设置颜色,但我相信还有其他方法。下面的示例基于假设,第一张幻灯片上有一个现有的PowerPoint文件,并且没有其他的饼图。显然,你会根据自己的情况进行调整。

using PowerPoint = Microsoft.Office.Interop.PowerPoint; 

namespace SampleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var filePath = @"C:\users\userx\desktop\test.pptx"; 
      var app = new PowerPoint.Application(); 
      var presentation = app.Presentations.Open(filePath); 
      var slide = presentation.Slides[1]; 
      var chart = slide.Shapes[1].Chart; 
      var series = chart.SeriesCollection(1) as PowerPoint.Series; 
      var point = series.Points(1) as PowerPoint.Point; 
      point.Interior.Color = 0x41BA5D; 
      point = series.Points(2) as PowerPoint.Point; 
      point.Interior.Color = 0xA841BA; 
      point = series.Points(3) as PowerPoint.Point; 
      point.Interior.Color = 0xBA4141; 
      point = series.Points(4) as PowerPoint.Point; 
      point.Interior.Color = 0x7AB4FF; 
     } 
    } 
} 

原来的饼图看起来像这样:

enter image description here

虽然新图表了这个样子:

enter image description here

正如我所提到的,有很多设置颜色的方法,我向你展示了十六进制方式。如果引用System.Drawing组装,那么你将有机会获得Color,从而简化了很多东西:

var point = series.Points(1) as PowerPoint.Point; 
point.Interior.Color = Color.Red; 
point = series.Points(2) as PowerPoint.Point; 
point.Interior.Color = Color.Pink; 
point = series.Points(3) as PowerPoint.Point; 
point.Interior.Color = Color.Black; 
point = series.Points(4) as PowerPoint.Point; 
point.Interior.Color = Color.Green; 

enter image description here

图例项将相应地改变它们的颜色,所以如果你使用这种方法,你甚至不必担心在那里设置颜色。

正如您所看到的,Interop可能是一种痛苦。希望这能为你解决一些问题。

+0

看起来很有希望。我会在早上尝试它,并让你知道。谢谢。 – Henry

+0

这使我得到我需要的东西。谢谢。 – Henry

+0

@Henry没问题。 –

相关问题