C#窗体中嵌入office文件

学习的日记,以后可能有用,先记录下来:在c#中嵌入打开office文件。在office2003中不会弹出打开提示,但据说在office2007中会弹出打开提示,不知道真的假的,有待确认(down个office2007麻烦啊!)。

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 System.Reflection;
using Microsoft.Office.Core;
//using Word;
//using AxShDocVw;
using SHDocVw;
using SonTaBll;


namespace SonTa
{
    public partial class frmClass : Form
    {
        public frmClass()
        {
            InitializeComponent();


        }

        private Object oDocument;
        public int type = -1;
        String strFileName = "";

        private void frmFirstClass_Load(object sender, EventArgs e)
        {
            OpenFile();
           
        }

        public void OpenFile()
        {

            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.AddExtension = true;
            ofDialog.CheckFileExists = true;
            ofDialog.CheckPathExists = true;
            ofDialog.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
            ofDialog.DefaultExt = "*.doc";
            if (ofDialog.ShowDialog() == DialogResult.OK)
            {
                strFileName = ofDialog.FileName;
            }


            if (strFileName.Length != 0)
            {
                Object refmissing = System.Reflection.Missing.Value;
                oDocument = null;
                axWebBrowser.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
            }

        }

        private void frmFirstClass_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.axWebBrowser.Dispose();
            oDocument = null;

        }

        private void axWebBrowser_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
        {
            //Note: You can use the reference to the document object to
            //      automate the document server.

            Object o = e.pDisp;

            oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);

            Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);

            Object oName = o.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, oApplication, null);

            //MessageBox.Show("File opened by: " + oName.ToString());
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值