C#窗体中嵌入office文件
2010-01-05 23:09
441 查看
学习的日记,以后可能有用,先记录下来:在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());
}
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());
}
相关文章推荐
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- c#在主窗体panel 容器内嵌入另一个窗体(子窗体)的实现
- C#窗体应用程序中 浏览本地文件 或 选择本地文件路径 的方法
- Winform窗体文本框实现拖拽获得目录或文件路径(C#)
- C# 把引用的dll嵌入到exe文件中
- C#程序如何把窗体文件从从一个项目中复制到另一个项目
- C# Vs2005资源文件嵌入资源使用方法
- C#中mvc模式在线预览Office(word 可编辑、txt)文件
- C#操作Office文件,成功释放,但读取文件时提示"文件***正由另一进程使用,因此该进程无法访问此文件"的解决办法
- C# 自定义窗体上Icon图标和修改默认启动的exe文件的图标
- C#操纵Excel,此工作薄包含嵌入对象,Office 2007的设定方法
- 用资源文件在C#程序中动态更换窗体背景...
- C#显示PDF文件,winform打开PDF文件并在窗体中显示
- c# 窗体 拖动 复制外部文件到路径
- 报表打印的实现(到打印机或Office文件上|用JS或C#)
- C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件。 ”的解决
- C#调用Matlab画图,figure嵌入到Winform窗体
- C#窗体中读取修改xml文件
- C# 将文件嵌入DLL 。Log4net 配置
- C#:自定义控件 资源文件 嵌入类型大全