• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c# 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论

这篇文章主要介绍了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)   <i>本文来源gaodai$ma#com搞$代*码*网</i> {      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的方法示例的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#中如何操作word的方法示例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址