WEB开发网
开发学院软件教学办公软件其他办公 PowerPoint2003上嵌入Excel Sheet 阅读

PowerPoint2003上嵌入Excel Sheet

 2012-06-26 15:02:52 来源:WEB开发网   
核心提示: 一下代码在PowerPoint2003的第二个Slide上嵌入一个Excel Sheet(有且只有一个Sheet)。using System;using System.IO;using System.Collections.Generic;using System.ComponentModel;using Syste

 一下代码在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();
            }
        }
    }
}

Tags:PowerPoint 嵌入 Excel

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接