在Powerpoint 2013中使用C#创建多个系列的图表 [英] Creating Charts with multiple series in Powerpoint 2013 using C#

查看:157
本文介绍了在Powerpoint 2013中使用C#创建多个系列的图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2013 Ultimate,与Office 2013(PowerPoint,Excel和Word全部安装)。我在C#中编码。

I am using VS2013 Ultimate, with Office 2013 (PowerPoint, Excel and Word all installed). I am coding in C#.

我正在使用C#创建PowerPoint演示文稿。到目前为止,我已经设法完成了我想做的一切。但是,尝试创建图表时遇到问题。我的理解是Excel用于管理数据(在工作表中)。我有以下代码,它将在PowerPoint幻灯片中生成两个系列的图表。问题是有时候图表没有添加到幻灯片。它被添加,工作表出现,数据被修改,图表从幻灯片中消失!我也注意到2个Excel实例的启动,但我不明白为什么。任何人都可以看清楚吗?感谢。

I am creating a PowerPoint presentation using C#. So far, I have managed to accomplish everything that I have wanted to do. However, I am having problems attempting to create a chart. My understanding is that Excel is used to manage the data (in a worksheet). I have the following code, which will produce a chart with two series in a PowerPoint slide. The problem is that 'sometimes' the chart does not get added to the slide. It gets added, the worksheet appears and the data is modified and the chart disappears from the slide! I also notice 2 instances of Excel firing up but I cannot understand why. Can anyone shed any light on this please? Thanks.

public void CreateChart(PPT.Slide slide)
    {
        slide.Layout = PPT.PpSlideLayout.ppLayoutBlank;

        var chart = slide.Shapes.AddChart(XlChartType.xlLine, 10f, 10f, 900f, 400f).Chart;

        var workbook = (EXCEL.Workbook)chart.ChartData.Workbook;
        workbook.Windows.Application.Visible = true;

        var dataSheet = (EXCEL.Worksheet)workbook.Worksheets[1];
        dataSheet.Cells.ClearContents();


        dataSheet.Cells.Range["A1"].Value2 = "Bananas";
        dataSheet.Cells.Range["A2"].Value2 = "Apples";
        dataSheet.Cells.Range["A3"].Value2 = "Pears";
        dataSheet.Cells.Range["A4"].Value2 = "Oranges";
        dataSheet.Cells.Range["B1"].Value2 = "1000";
        dataSheet.Cells.Range["B2"].Value2 = "2500";
        dataSheet.Cells.Range["B3"].Value2 = "4000";
        dataSheet.Cells.Range["B4"].Value2 = "3000";

        var sc = (PPT.SeriesCollection)chart.SeriesCollection();

        do
        {
            var seriesToDelete = sc.Item(1);
            seriesToDelete.Delete();
        }
        while (sc.Count != 0);

        var series1 = sc.NewSeries();
        series1.Name = "Pauls Series";
        series1.XValues = "'Sheet1'!$A$1:$A$2";
        series1.Values = "'Sheet1'!$B$1:$B$2";
        series1.ChartType = XlChartType.xlLine;

        var series2 = sc.NewSeries();
        series2.Name = "Seans Series";
        series2.XValues = "'Sheet1'!$A$1:$A$2";
        series2.Values = "'Sheet1'!$B$3:$B$4";
        series2.ChartType = XlChartType.xlLine; 

        chart.HasTitle = true;
        chart.ChartTitle.Font.Italic = true;
        chart.ChartTitle.Text = "My First Chart!";
        chart.ChartTitle.Font.Size = 12;
        chart.ChartTitle.Font.Color = Color.Black.ToArgb();
        chart.ChartTitle.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
        chart.ChartTitle.Format.Line.ForeColor.RGB = Color.Black.ToArgb();

        chart.HasLegend = true;
        chart.Legend.Font.Italic = true;
        chart.Legend.Font.Size = 10;

        chart.Refresh();


    }


推荐答案

p>所以,在Do {}块中放置一个chart.refresh()已经解决了我的问题!多么奇怪啊!

so, putting a chart.refresh() within the Do{} block has resolved my problems! How odd!?!

这篇关于在Powerpoint 2013中使用C#创建多个系列的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆