资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#中操作word的案例-创新互联

这篇文章主要介绍了C#中操作word的案例,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

创新互联于2013年创立,是专业互联网技术服务公司,拥有项目成都网站设计、网站制作、外贸营销网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元德钦做网站,已为上家服务,为德钦各地企业和个人服务,联系电话:18980820575
#region 读取word
/// 
/// 读取word所有文字内容(不包含表格)
/// 
/// word中的字符内容(纯文本)
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 /// 
/// 追加写入word
/// 
/// 需要写入的字符串
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的案例内容对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,遇到问题就找创新互联,详细的解决方法等着你来学习!


当前题目:C#中操作word的案例-创新互联
转载来于:http://cdkjz.cn/article/cdcccp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220