美图齐众专注资阳网站设计 资阳网站制作 资阳网站建设
资阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

浅析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文档有所帮助。


当前文章:浅析C#打开Word文档实例
当前链接:http://www.zsjierui.cn/article/cdhpsci.html

其他资讯