从ppt演示文稿中选择某个幻灯片并粘贴excel图表

时间:2013-10-24 23:05:56

标签: c# visual-studio-2010 excel powerpoint

我有以下代码来打开工作表,复制一个图表,打开演示文稿并粘贴它。

它工作正常,对于一张图表和一张幻灯片,但在XLSM中有8个图表,所以8个幻灯片在PPTX中,我不知道如何选择例如,第二个图表并将其粘贴到第二个或第三个演示文稿的幻灯片。

使用 PowerPoint.Slide curSlide = pptApp.ActiveWindow.View.Slide; ,它会选择当前幻灯片或幻灯片1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core;
using xlNS = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Graph = Microsoft.Office.Interop.Graph;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication3
{
   public partial class Form1 : Form
   {
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            xlNS.Application excelApplication = null;
            xlNS.Workbook excelWorkBook = null;
            xlNS.Worksheet targetSheet = null;
            xlNS.ChartObjects chartObjects = null;
            xlNS.ChartObject existingChartObject = null;

            String Excelpath = "C:\\Users\\Diego\\Desktop\\Indicador Mensal.xlsm";
            excelApplication = new xlNS.Application();//Instancia o excel e abre o XLSM
            excelWorkBook = excelApplication.Workbooks.Open(Excelpath,
                 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                 Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            PowerPoint.Application pptApp = new PowerPoint.Application();
            pptApp.Presentations.Open("C:\\Users\\Diego\\Desktop\\Teste.pptx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue); //Abre o PPTX
            PowerPoint.Slide curSlide = pptApp.ActiveWindow.View.Slide;

            xlNS.Worksheet Ws = new xlNS.Worksheet();
            Ws = (xlNS.Worksheet)excelWorkBook.Worksheets[1];//Número da Planilha que contém o gráfico
            Ws.Activate();

            targetSheet = (xlNS.Worksheet)(excelWorkBook.Worksheets["Assumidos no Prazo"]);
            chartObjects = (xlNS.ChartObjects)(targetSheet.ChartObjects(Type.Missing));
            existingChartObject = (xlNS.ChartObject)(chartObjects.Item(1));
            existingChartObject.Copy();
            curSlide.Shapes.Paste();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            MessageBox.Show("Finalizado");
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

使用此代码解决了问题:

p = pptApp.ActivePresentation; 
slides = p.Slides; 
slides[3].Select(); 
Ppt.Slide slide3 = pptApp.ActiveWindow.View.Slide;