欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#操作word的方法示例

程序员文章站 2023-12-03 17:04:58
本文实例讲述了c#操作word的方法。分享给大家供大家参考,具体如下: #region 读取word /// /// 读取wo...

本文实例讲述了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#相关内容感兴趣的读者可查看本站专题:《c#操作excel技巧总结》、《c#程序设计之线程使用技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

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