1.c#,winForm嵌入word并能操作文档
在 Visual C# .NET 中新建一个 Windows 应用程序项目。
默认情况下会创建 Form1。 在工具菜单上,单击自定义工具箱以打开自定义工具箱对话框。
在 COM 组件选项卡上,添加一个对 Microsoft WebBrowser 的引用。单击确定,将 WebBrowser 控件添加到 Windows 窗体工具箱。
WebBrowser 控件会显示出来,并且在工具箱中带有 Explorer(资源管理器)字样。 使用该工具箱向 Form1 添加一个 WebBrowser 控件、一个 OpenFileDialog 控件和一个 CommandButton 按钮。
这就会向 Form1 类添加 AxWebBrowser1、OpenFileDialog1 和 Button1 成员变量。 在 Form1 上,双击 button1。
这就会向 Form1 添加 Button1_Click 事件。 在 Form1 的代码窗口中,向列表添加以下命名空间:using System.Reflection; 如下所示在 Form1 类中定义一个私有成员:private Object oDocument; 在 Form1 类的 InitializeComponent 方法的末尾,添加以下代码以处理 Form1_Load、Form1_Closed 和 axWebBrowser1_NavigateComplete2 事件:this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2); this.Load += new System.EventHandler(this.Form1_Load); this.Closed += new System.EventHandler(this.Form1_Closed); 将下面的代码private void button1_Click(object sender, System.EventArgs e) { } 替换为: private void button1_Click(object sender, System.EventArgs e) { String strFileName; //Find the Office document. openFileDialog1.FileName = ""; openFileDialog1.ShowDialog(); strFileName = openFileDialog1.FileName; //If the user does not cancel, open the document. if(strFileName.Length != 0) { Object refmissing = System.Reflection.Missing.Value; oDocument = null; axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing , ref refmissing , ref refmissing); } } public void Form1_Load(object sender, System.EventArgs e) { button1.Text = "Browse"; openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ; openFileDialog1.FilterIndex = 1; } public void Form1_Closed(object sender, System.EventArgs e) { oDocument = null; } public void axWebBrowser1_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() ); } 按 F5 键运行该项目。
单击浏览后,会出现打开对话框,您可以使用该对话框浏览到 Word 文档、Excel 工作表或 PowerPoint 演示文稿。选择任一文件,然后单击打开。
文档在 WebBrowser 控件内打开,并出现一个显示有 Office 文档服务器名称的消息框。
2.c# winform 获取Word数据位置
//书签位置获取
public MSWord.Range GetMarkRange(string sBookmarks)
{
try
{
MSWord.Range result = null;
foreach (MSWord.Bookmark book in xWordDoc.Bookmarks)
{
if (book.Name == sBookmarks)
{
result=book.Range;
break;
}
}
return result;
}
catch (Exception error)
{
throw error;
}
}
string sBookmarks="书签位置"
MSWord.Range range = null;
try
{
range = GetMarkRange(sBookmarks);
range.Tables.Add();// range.Tables 操作table
}
catch (Exception error)
{
throw error;
}
finally
{
range = null;
}
3.如何用c#winform做一个文本编辑器
其实也就是操作流 把I/O工作流理解了 文本编辑器也没啥难的
关键类 FileStream
这是一个简单打开文件的功能
tbContent.Text = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
OpenWord(openFileDialog1.FileName);
///rtbContent就是richtextbox控件
rtbContent.Text = File.ReadAllText(openFileDialog1.FileName, Encoding.Default);
}
4.紧急求助,在WinForm中 Word文档在线预览
通过网页的形式来打开PPT,要想实现这样的效果,必须将PPT文件转换成为网页文件,操作方法很简单,如下:打开您的PPT文件,执行如下图的操作!
1.上图中,执行“文件”→“另外为网页”,之后选择好保存的路径及文件名称即可。
保存成功之后,我们会得到一个扩展名为mht的文件,如下图!
2.使用鼠标直接点击如上图的文件,即可通过网页的形式打开PPT文件,如下图!
3.通过网页打开PPT,要想观看幻灯片效果,操作方式和原幻灯片基本无较大的区别,左边是幻灯片的序号,右边显示幻灯片内容。但是,其显示效果没有原来的演示文稿好!
转载请注明出处51数据库 » winform仿word