C#操作word文档小结

  C#操作word文档,需要添加word文档COM dll引用

1、引用

    需要引用 COM库:Microsoft word 11.0 Object Library. 不同的版本,会有不同的版本号。

   如 2010版Office 就是 Microsoft word 14.0 Object Library.

2、引用相应的名字空间:

    using Microsoft.Office.Core;
    using word = Microsoft.Office.Interop.Word;

3、打开一个已存在的word文档

        public void OpenDocFile(string docName)
        {
            
        object oMissing = System.Reflection.Missing.Value;  //一个编程时需要经常使用的一个参数
        word.ApplicationClass wordapp = null;   //这是WORD程序,在这个程序下,可以同时打开多个文档,尽量不要同时打开多个Word程序,否则会出错的。
        word.Document doc = null;  //第一个需要打开的WORD文档
        word.Document doc2 = null;  //另一个需要打开的WORD文档
        wordapp = new word.ApplicationClass();
        wordapp.Visible = true; //所打开的WORD程序,是否是可见的。
            object docObject = docName;  //由于COM操作中,都是使用的 object ,所以,需要做一些转变                       
         if (File.Exists(docName))   // 如果要打开的文件名存在,那就使用doc来打开 
            {
                doc = wordapp.Documents.Add(ref docObject, ref oMissing, ref oMissing, ref oMissing);
                doc.Activate();   //将当前文件设定为活动文档
                ParagraphsCount = doc.Content.Paragraphs.Count;   //此文档中,段落的数量,也就是这个文档中,有几个段落。
            }
            else   //如果文件名不存在,那就使用doc2来打开
            {
                doc2 = wordapp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                DocFileName = docName;
                doc2.Activate();   //将当前文件设定为活动文档 
            }
                  
        }

 4、获取某一段的具体文本内容

        public string Paragraph(int index)
        {
            word.Paragraph   para;  
            para = doc.Content.Paragraphs[index];   ///这是一个设定对应的某一段             
            return para.Range.Text;
        }

 5、DOC内容借助于剪贴板的复制与粘贴

        /// <summary>
        /// 将doc某一段的内容复制到剪贴板上
        /// </summary>
        /// <param name="index">段的序号</param>
        public void CopyParagraph( int index)
        {
            word.Paragraph para;
            para = doc.Content.Paragraphs[index];
            para.Range.Copy();
        
        }
        /// <summary>
        /// 将剪贴板的内容粘贴到doc2文档中
        /// </summary>
        public void PasteParagraph()
        {
            if (doc2 != null)
            {
                word.Paragraph para = doc2.Content.Paragraphs.Add(ref oMissing);
                try
                {
                    para.Range.Paste();
                    para.Range.InsertParagraphBefore();//添加一次回车
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }

6、关闭WORD程序和文档

        /// <summary>
        /// 关闭 word程序
        /// </summary>
        public void CloseWord()
        {
            if (wordapp != null)
            {
                if (doc != null)
                {
                    word._Document docc = doc as word._Document;
                    docc.Close(ref oMissing, ref oMissing, ref oMissing);
                }
                if (doc2 != null)
                {
                    word._Document docc = doc2 as word._Document;
                    docc.Close(ref oMissing, ref oMissing, ref oMissing);
                }
                wordapp.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }

7、替换文档中的内容

       /// <summary>
        /// 替换文档中的内容
        /// </summary>
        /// <param name="oldString">原有内容</param>
        /// <param name="newString">替换后的内容</param>
        public void Replace(string oldString, string newString)
        {
            doc2.Content.Find.Text = oldString;
            object FindText, ReplaceWith, ReplaceAll;
            FindText = oldString;
            ReplaceWith = newString;
            ReplaceAll = word.WdReplace.wdReplaceAll;
            doc2.Content.Find.Execute(ref FindText, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref ReplaceWith, 
                                      ref ReplaceAll, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing, 
                                      ref oMissing);
        }

 

以上,如果要替换其中的回车符为空格,可以这样使用

Replace(“^p”,"");

就可以了,这一点,与在WORD中使用的替换功能是一样的。

8、保存文档内容

        /// <summary>
        /// 保存word文档(只保存doc2)
        /// </summary>
        public void SaveDocFile()
        {
            if (doc2 != null)
            {
                if (!string.IsNullOrEmpty(DocFileName))
                {
                    object docname = DocFileName;//要保存的文件名称,包括路径
                    Replace("^p^p", "^p");
                    doc2.SaveAs2(ref docname, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing,
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing, 
                                 ref oMissing,
                                 ref oMissing,
                                 ref oMissing);
                }
            }
        
        }

 9、在复制过程中,如果文档中有表格,那么,上面所使用的复制方法,一般会报错。“"此方法或属性无效,因为 对象涉及表格行尾"。出现的原因就是因为 Paragraphs 只能是文字内容不能包括表格,在WORD中,表格是另一类重要的文档类型。其实在表格中,每一个单元格都是一个独立的段落。所以,如果文档中有表格,那么 文档的 doc.Content.Paragraphs.Count 数量会增加很多。在复制过程中,可以采用下面的方法来复制。

         /// <summary>
          /// 复制文档的内容,从开始到结束(包括结束与开始的段落)的段落内容。
          /// </summary>
          /// <param name="first">开始的段落号</param>
          /// <param name="next">结束的段落号</param>
          public void CopyParagraph2(int first,int next)
          {
              word.Range range = doc.Range();
  
             word.Paragraph para1;
             para1 = doc.Content.Paragraphs[first];
             range.Start = para1.Range.Start;
 
             word.Paragraph para2;
             para2 = doc.Content.Paragraphs[next];
             range.End = para2.Range.End;
 
             range.Copy();
         
         }

  在这个复制过程中,重新建立了一个区域 word.Range range = doc.Range(); 这个区域包括文档的所有内容。

  然后根据段落的要求分别设定区域的Start  和 End ,如果在 Start  和 End 之间有一个表格的话,那么表格也会被正确复制过去。

来源:http://www.cnblogs.com/lujin49/archive/2011/08/24/2152505.html

加支付宝好友偷能量挖...


评论(0)网络
阅读(248)喜欢(0)Asp.Net/C#/WCF