C#操作Word学习实例浅析

开发 后端
C#操作Word学习实例主要向你介绍了C#操作Word学习中的一个练习,希望对你了解和学习C#操作Word有所帮助。

C#操作Word学习实例:首先引入类库,Microsoft.Office.Interop.Word,然后进行编程。代码如下:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Text;   
  7. using System.Windows.Forms;   
  8. using Microsoft.Office.Interop.Word;   
  9.  
  10. namespace WordTest   
  11. {   
  12. public partial class Form1 : Form   
  13. {   
  14. object strFileName;   
  15. Object Nothing;   
  16. Microsoft.Office.Interop.Word.Application myWordApp =  
  17.  new Microsoft.Office.Interop.Word.ApplicationClass();   
  18. Document myWordDoc;   
  19. string strContent = "";   
  20.  
  21. public Form1()   
  22. {   
  23. InitializeComponent();   
  24. }   //C#操作Word学习实例浅析
  25.  
  26. private void button1_Click(object sender, EventArgs e)   
  27. {   
  28. createWord();   
  29. //openWord();   
  30. }   
  31.  
  32. private void createWord()   
  33. {   
  34. strFileName = System.Windows.Forms.Application.StartupPath + "test.doc";   
  35. if (System.IO.File.Exists((string)strFileName))   
  36. System.IO.File.Delete((string)strFileName);   
  37. Object Nothing = System.Reflection.Missing.Value;   
  38. myWordDoc = myWordApp.Documents.Add(ref Nothing,   
  39. ref Nothing, ref Nothing, ref Nothing);   
  40.  
  41. #region 将数据库中读取得数据写入到word文件中   
  42.  
  43. strContent = "你好nnr";   
  44. myWordDoc.Paragraphs.Last.Range.Text = strContent;   
  45.  
  46. strContent = "这是测试程序";   
  47. myWordDoc.Paragraphs.Last.Range.Text = strContent;   
  48.  //C#操作Word学习实例浅析
  49.  
  50. #endregion   
  51.  
  52. //将WordDoc文档对象的内容保存为DOC文档   
  53. myWordDoc.SaveAs(ref strFileName, ref Nothing,   
  54. ref Nothing, ref Nothing, ref Nothing, ref Nothing,  
  55.  ref Nothing, ref Nothing, ref Nothing, ref Nothing,  
  56.  ref Nothing, ref Nothing, ref Nothing, ref Nothing,  
  57.  ref Nothing, ref Nothing);   
  58. //关闭WordDoc文档对象   
  59. myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);   
  60. //关闭WordApp组件对象   
  61. myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   
  62.  
  63. this.richTextBox1.Text = strFileName + "rn" + "创建成功";   
  64.  
  65. }   
  66. private void openWord()   
  67. {   //C#操作Word学习实例浅析
  68. fontDialog1.ShowDialog();   
  69. System.Drawing.Font font = fontDialog1.Font;   
  70. object filepath = "D:asp.docx";   
  71. object oMissing = System.Reflection.Missing.Value;   
  72. myWordDoc = myWordApp.Documents.Open(ref filepath,  
  73.  ref oMissing, ref oMissing, ref oMissing,  
  74.  ref oMissing,   
  75. ref oMissing, ref oMissing, ref oMissing,  
  76.  ref oMissing, ref oMissing, ref oMissing,   
  77. ref oMissing, ref oMissing, ref oMissing,  
  78.  ref oMissing, ref oMissing);   
  79. myWordDoc.Content.Font.Size = font.Size;   
  80. myWordDoc.Content.Font.Name = font.Name;   
  81. myWordDoc.Save();   
  82. richTextBox1.Text = myWordDoc.Content.Text;   
  83.  
  84.  //C#操作Word学习实例浅析
  85. myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);   
  86. myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);   
  87. }   
  88.  
  89. }  

C#操作Word学习实例的基本内容就向你介绍到这里,希望对你了解和学习C#操作Word有所帮助。

【编辑推荐】

  1. C#操作Word表的实例浅析
  2. C#操作Word表格的常见操作
  3. C#操作Word表格的彪悍实例
  4. C#操作Word实用实例浅析
  5. C#操作Word的一点认识
责任编辑:仲衡 来源: itpub.net
相关推荐

2009-08-19 11:13:49

C#操作Word

2009-08-19 09:42:52

C#操作Word书签

2009-08-19 10:25:14

C#操作Word

2009-08-19 11:28:41

C#操作Word

2009-09-01 13:13:28

C#打开Word文档

2009-08-28 17:34:14

读取word文档

2009-08-18 13:49:21

C# 操作Excel

2009-08-31 18:38:59

C#写文件

2009-08-18 16:04:12

C# 操作Excel

2009-08-20 11:07:07

C#共享内存

2009-08-26 13:48:31

C#打印条码

2009-08-19 10:46:48

C#操作Word表格

2009-08-27 13:30:11

C# interfac

2009-08-18 17:42:12

C#操作符重载

2009-08-19 16:30:55

C#操作Access数

2009-08-19 14:12:23

C#操作注册表

2009-08-19 17:44:15

C#操作文本文件

2009-08-27 17:59:56

C#接口定义

2009-08-18 16:49:05

C# 操作XML

2009-08-17 13:34:02

C#异步操作
点赞
收藏

51CTO技术栈公众号