浅析C#打开Word文档实例

开发 后端
C#打开Word文档需要使用什么类呢?具体的C#打开Word文档实现过程以及步骤和注意事项是什么呢?希望本文介绍的内容随你有所帮助。

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

C#打开Word文档实例如下:

  1. //在项目引用里添加上对Microsoft Word 11.0 object library的引用  
  2.  
  3. private void button1_Click(object sender, System.EventArgs e)  
  4. {  
  5. //调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称  
  6. OpenFileDialog opd = new OpenFileDialog();  
  7. opd.InitialDirectory = \"c:\\\\\";  
  8. opd.Filter =   
  9. \"Word文档(*.doc)|*.doc|文本文档(*.txt)|  
  10. *.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*\";  
  11. opd.FilterIndex = 1;  
  12.  
  13. if (opd.ShowDialog() ==   
  14. DialogResult.OK && opd.FileName.Length > 0)  
  15. {  
  16.  
  17. //建立Word类的实例,缺点:不能正确读取表格,图片等等的显示  
  18. Word.ApplicationClass app = new Word.ApplicationClass();  
  19. Word.Document doc = null;  
  20. object missing = System.Reflection.Missing.Value;  
  21.  
  22. object FileName = opd.FileName;  
  23. object readOnly = false;  
  24. object isVisible = true;  
  25. object index = 0;  
  26. try 
  27. {  
  28. doc = app.Documents.Open(  
  29. ref FileName, ref missing, ref readOnly,  
  30. ref missing, ref missing,   
  31. ref missing, ref missing, ref missing,  
  32. ref missing, ref missing,   
  33. ref missing, ref isVisible, ref missing,  
  34. ref missing, ref missing, ref missing);  
  35.  
  36. doc.ActiveWindow.Selection.WholeStory();  
  37. doc.ActiveWindow.Selection.Copy();  
  38. //从剪切板获取数据  
  39. IDataObject data=Clipboard.GetDataObject();  
  40. this.richTextBox1.Text=  
  41. data.GetData(DataFormats.Text).ToString();  
  42.  
  43. }  
  44. finally 
  45. {  
  46. if (doc != null)  
  47. {  
  48. doc.Close(ref missing, ref missing, ref missing);  
  49. doc = null;  
  50. }  
  51.  
  52. if (app != null)  
  53. {  
  54. app.Quit(ref missing, ref missing, ref missing);  
  55. app = null;[Page]  
  56. }  
  57. }  
  58.  
  59. }  
  60.  
  61. }  
  62.  

C#打开Word文档的具体实现的基本内容就向你介绍到这里,希望对你了解和学习C#打开Word文档有所帮助。

【编辑推荐】

  1. C#项目初期准备工作浅析
  2. C#项目的创建过程详解
  3. 详解C#读取word内容操作
  4. C#读取Word文件实例详解
  5. C#读取Word学习经验总结
责任编辑:仲衡 来源: e800.com.cn
相关推荐

2009-08-28 17:34:14

读取word文档

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2009-08-19 09:42:52

C#操作Word书签

2009-08-12 15:26:38

C#读取XML文档

2009-09-01 13:51:51

C#创建Word文档

2009-08-19 10:25:14

C#操作Word

2009-08-19 11:28:41

C#操作Word

2009-09-27 10:43:13

C#合并多个WORD文

2009-08-27 13:30:11

C# interfac

2009-08-19 10:16:15

C#操作Word

2009-09-01 13:25:25

C#Word文档替换

2009-08-18 13:49:21

C# 操作Excel

2009-08-27 17:59:56

C#接口定义

2009-08-17 17:49:20

C# 枚举

2009-09-09 13:57:28

C# XML解析

2009-09-03 14:55:34

C#计算时间间隔

2009-08-31 18:38:59

C#写文件

2009-08-18 16:04:12

C# 操作Excel

2009-08-27 17:11:44

C# Fluent I
点赞
收藏

51CTO技术栈公众号