C#打开Word文档内容并显示是如何实现的呢?让我们通过C#打开Word文档的实现代码来学习C#打开Word文档的具体过程和注意事项以及在C#打开Word文档过程中涉及到的类的使用,希望对你有所帮助。

C#打开Word文档实例如下:
- //在项目引用里添加上对Microsoft Word 11.0 object library的引用
 - private void button1_Click(object sender, System.EventArgs e)
 - {
 - //调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称
 - OpenFileDialog opd = new OpenFileDialog();
 - opd.InitialDirectory = \"c:\\\\\";
 - opd.Filter =
 - \"Word文档(*.doc)|*.doc|文本文档(*.txt)|
 - *.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*\";
 - opd.FilterIndex = 1;
 - if (opd.ShowDialog() ==
 - DialogResult.OK && opd.FileName.Length > 0)
 - {
 - //建立Word类的实例,缺点:不能正确读取表格,图片等等的显示
 - Word.ApplicationClass app = new Word.ApplicationClass();
 - Word.Document doc = null;
 - object missing = System.Reflection.Missing.Value;
 - object FileName = opd.FileName;
 - object readOnly = false;
 - object isVisible = true;
 - object index = 0;
 - try
 - {
 - doc = app.Documents.Open(
 - ref FileName, ref missing, ref readOnly,
 - ref missing, ref missing,
 - ref missing, ref missing, ref missing,
 - ref missing, ref missing,
 - ref missing, ref isVisible, ref missing,
 - ref missing, ref missing, ref missing);
 - doc.ActiveWindow.Selection.WholeStory();
 - doc.ActiveWindow.Selection.Copy();
 - //从剪切板获取数据
 - IDataObject data=Clipboard.GetDataObject();
 - this.richTextBox1.Text=
 - data.GetData(DataFormats.Text).ToString();
 - }
 - finally
 - {
 - if (doc != null)
 - {
 - doc.Close(ref missing, ref missing, ref missing);
 - doc = null;
 - }
 - if (app != null)
 - {
 - app.Quit(ref missing, ref missing, ref missing);
 - app = null;[Page]
 - }
 - }
 - }
 - }
 
C#打开Word文档的具体实现的基本内容就向你介绍到这里,希望对你了解和学习C#打开Word文档有所帮助。
                文章题目:浅析C#打开Word文档实例
                
                转载注明:http://www.csdahua.cn/qtweb/news19/420419.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网