Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言访问PowerPoint图表#_C#_Charts_Powerpoint_Office Interop - Fatal编程技术网

C# 用C语言访问PowerPoint图表#

C# 用C语言访问PowerPoint图表#,c#,charts,powerpoint,office-interop,C#,Charts,Powerpoint,Office Interop,我在一个C#项目中遇到了一个问题。事实上,我创建了一个PowerPoint插件,我想在幻灯片上生成图表 我制作了一张幻灯片,其中包括: using PowerPoint = Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Interop.Graph; Microsoft.Office.Interop.Graph.Chart objChart; objChart = (Microsoft.Office.Interop.Gra

我在一个C#项目中遇到了一个问题。事实上,我创建了一个PowerPoint插件,我想在幻灯片上生成图表

我制作了一张幻灯片,其中包括:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Graph;

Microsoft.Office.Interop.Graph.Chart objChart;
objChart = (Microsoft.Office.Interop.Graph.Chart)objShape.OLEFormat.Object;`
图表是在幻灯片上创建的,但我无法访问要更新或插入的数据

我已尝试使用如下数据表:

//DataSheet test = objChart.Application.DataSheet;
//test.Cells.Clear()

这删除了图表的数据,但我不知道以后如何将值插入到图表数据中。

您是否尝试过使用OpenXML SDK,我用它构建复杂的Excel表格和Word文档,我相信它也支持Powerpoint。
请参阅。

好的,因此对于初学者,请确保包括以下参考:

  • 从.Net库:
    • Microsoft.Office.Interop.Graph
    • Microsoft.Office.Interop.Powerpoint
  • 从COM库:
    • Microsoft Office XX对象库(其中XX是您所在组织使用最广泛的Office版本[如果您将其包含在软件包中,则实际上并不重要])
将此添加到您的声明部分:

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using Core = Microsoft.Office.Core;
然后,尝试以下代码段:

PowerPoint.Application app = new PowerPoint.Application();
app.Visible = Core.MsoTriState.msoTrue; // Sure, let's watch the magic as it happens.

PowerPoint.Presentation pres = app.Presentations.Add();
PowerPoint._Slide objSlide = pres.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutTitleOnly);

PowerPoint.TextRange textRange = objSlide.Shapes[1].TextFrame.TextRange;
textRange.Text = "My Chart";
textRange.Font.Name = "Comic Sans MS";  // Oh yeah I did
textRange.Font.Size = 24;
Graph.Chart objChart = (Graph.Chart)objSlide.Shapes.AddOLEObject(150, 150, 480, 320,
    "MSGraph.Chart.8", "", Core.MsoTriState.msoFalse, "", 0, "", 
    Core.MsoTriState.msoFalse).OLEFormat.Object;

objChart.ChartType = Graph.XlChartType.xl3DPie;
objChart.Legend.Position = Graph.XlLegendPosition.xlLegendPositionBottom;
objChart.HasTitle = true;
objChart.ChartTitle.Text = "Sales for Black Programming & Assoc.";  // I'm a regular comedian.
应该像冠军一样工作。我希望这有帮助。

您也可以使用

PowerPoint.Chart
如果您使用的是Office 10或更高版本,则此选项可用

这是密码

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
然后,在函数外部声明这些对象

PowerPoint.Slide pSlide = null;
PowerPoint.Shape pShape = null;
PowerPoint.Chart pChart = null;
在函数定义中

pSlide = this.Application.ActivePresentation.Slides[1];
pShape = slide.Shapes.AddChart(Office.XlChartType.xlColumnStacked, 200, 200, 300, 300);//These values tell where the chart will be positioned
pChart = pShape.Chart;
现在,为了访问图表数据,必须创建excel工作簿和工作表对象

PowerPoint.ChartData pChartData = pChart.ChartData;
Excel.Workbook eWorkbook = (Excel.Workbook)pChartData.Workbook;
Excel.Worksheet eWorksheet = (Excel.Worksheet)eWorkbook.Worksheets[1];
现在,您可以通过以下方式访问图表数据:

(Excel.Range)eWorksheet.Cells.get_Range("A1", missing).get_Value();

在引用现有图表的工作簿属性之前调用……CARTDATA。激活方法< /P>

请尝试阅读本文档: