C# winform如何实现在控件中显示word文档?

yuchengxujieyuan yuchengxujieyuan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这是我在网上查到的代码,不知道为什么无法显示在控件里,而是直接代开了word文档
请高手指点!
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;

namespace TestShowWordOnForm
{
public partial class Form1 : Form
{
private Object oDocument;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
String strFileName;
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
strFileName = openFileDialog1.FileName;
if (strFileName.Length != 0)
{
Object refmissing = System.Reflection.Missing.Value;
oDocument = null;
axWebBrowser1.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
}


}

private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "Browse";
openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
openFileDialog1.FilterIndex = 1;

}

private void Form1_Closed(object sender, EventArgs e)
{
oDocument = null;
}

private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
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());

}
}
}
...全文
评论 6
收藏 已收藏
分享
返回顶部
yuchengxujieyuan
C# 111,129 成员 642,540 内容