C#操作word的方法示例

时间:2021-09-27 07:33:48

本文实例讲述了C#操作word的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#region 读取word
/// <summary>
/// 读取word所有文字内容(不包含表格)
/// </summary>
/// <returns>word中的字符内容(纯文本)</returns>
public string ReadAllFromWord()
{
  Word.ApplicationClass app = null;
  Word.Document doc = null;
  object missing = System.Reflection.Missing.Value;
  object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
  object readOnly = true;
  object isVisible = false;
  try
  {
    app = new Word.ApplicationClass();
    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);
    string textString = "";
    //读取全部内容 何问起 hovertree.com
    textString = doc.Content.Text.Trim();
//        int ParCount = this.getParCount(doc);//段数
//        for (int i = 1 ; i <= ParCount ; i++)
//        {
//          textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
//        }
    textString = textString.Replace("/a","");  //替换空串为空。(word中/a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
    textString = textString.Replace("/r","/n");  //替换回车为回车换行
    return textString;
  }
  catch(Exception ex)
  {
    throw ex;
  }
  finally
  {
    if (doc != null)
    {
      try
      {
        doc.Close(ref missing, ref missing, ref missing);
      }
      catch
      {}
      doc = null;
    }
    if (app != null)
    {
      try
      {
        app.Quit(ref missing, ref missing, ref missing);
      }
      catch
      {}
      app = null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
  }
}
#endregion
#region 追加写入word /// <summary>
/// 追加写入word
/// </summary>
/// <param name="InsertText">需要写入的字符串</param>
public void WriteToWord(string InsertText)
{
  Word.ApplicationClass app = null;
  Word.Document doc = null;
  object missing = System.Reflection.Missing.Value;
  object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
  object readOnly = false;
  object isVisible = false;
  try
  {
    app = new Word.ApplicationClass();
    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);
    //激活word文档
    doc.Activate();
    //追加到最后一段(段落是按照 /n 来作为标志的)
    doc.Paragraphs.Last.Range.Text = InsertText + "/n";//加个结束符(增加一段),否则再次插入的时候就成了替换.
    //保存
    doc.Save();
  }
  catch(Exception ex)
  {
    throw ex;
  }
  finally
  {
    if (doc != null)
    {
      try
      {
        doc.Close(ref missing, ref missing, ref missing);
      }
      catch
      {}
      doc = null;
    }
    if (app != null)
    {
      try
      {
        app.Quit(ref missing, ref missing, ref missing);
      }
      catch
      {}
      app = null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
  }
}
#endregion

希望本文所述对大家C#程序设计有所帮助。