C#中如何操作word的方法範例

黄舟
發布: 2017-05-14 10:34:03
原創
1668 人瀏覽過

這篇文章主要介紹了C#操作word的方法,結合實例形式分析了C#針對word文檔的讀取、寫入、保存等相關操作技巧,需要的朋友可以參考下

#本文實例講述了C#操作word的方法。分享給大家參考,具體如下:


#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#中如何操作word的方法範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板