PowerPoint2003上嵌入Excel Sheet

一下代码在PowerPoint2003的第二个Slide上嵌入一个Excel Sheet(有且只有一个Sheet)。

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

namespace PPTEmbedWorkSheet
{
    public partial class Form1 : Form
    {
        private OpenFileDialog OFD = new OpenFileDialog();
        private string _PPTPath;

        public Form1()
        {
            InitializeComponent();
            OFD.Multiselect = false;
            button2.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OFD.Filter = "PowerPoint Presentation (2003)|*.ppt";
            OFD.ShowDialog();
            _PPTPath = OFD.FileName;
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OFD.Filter = "Excel Workbook (2003)|*.xls";
            OFD.ShowDialog();
            string Path = OFD.FileName;
            string temp = AppDomain.CurrentDomain.BaseDirectory + "temp.xls";
            File.Copy(Path, temp, true);
            Excel.Application xlApp = new Excel.Application();
            var missing = Type.Missing;
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(temp, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing, missing);
            Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets[2];
            xlApp.Visible = true;
           
            foreach (var obj in xlWorkbook.Sheets)
            {
                Excel.Worksheet ws = (Excel.Worksheet)obj;
                if (!ws.Name.Equals(xlWorksheet.Name))
                {
                    ws.Delete();
                }
            }
            xlWorkbook.Save();
            xlWorkbook.Close(missing, missing, missing);
            xlApp.Quit();
            PowerPoint.Application ppApp = new PowerPoint.Application();
            PowerPoint.Presentation ppP = ppApp.Presentations.Open(_PPTPath, Office.MsoTriState.msoFalse,
                Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse);
            ppApp.Visible = Office.MsoTriState.msoTrue;
            ppP.NewWindow();
            PowerPoint.Slide ppS = ppP.Slides[2];
            try
            {
                /*这里一定不能有ClassName否则会报错*/
                PowerPoint.Shape shap = ppS.Shapes.AddOLEObject(0, 0, 200, 100,
                    "", temp, Office.MsoTriState.msoFalse, temp, 1,
                    "test", Office.MsoTriState.msoFalse);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                File.Delete(temp);
                Close();
            }
        }
    }
}



你可能感兴趣的:(Excel)