一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net C# word文件的操作

时间:2022-06-25 08:26:15 编辑:袖梨 来源:一聚教程网

asp教程.net C# word文件的操作
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内容借助于剪贴板的复制与粘贴

 

        ///


        /// 将doc某一段的内容复制到剪贴板上
        ///

        /// 段的序号
        public void CopyParagraph( int index)
        {
            word.Paragraph para;
            para = doc.Content.Paragraphs[index];
            para.Range.Copy();
       
        }
        ///
        /// 将剪贴板的内容粘贴到doc2文档中
        ///

        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程序和文档

 

        ///


        /// 关闭 word程序
        ///

        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、替换文档中的内容

 

       ///


        /// 替换文档中的内容
        ///

        /// 原有内容
        /// 替换后的内容
        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、保存文档内容

 

        ///


        /// 保存word文档(只保存doc2)
        ///

        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);
                }
            }
       
        }
从网上查了很多资料,发现,许多都是重复的。更有许多知识,根本没有讲到。为了以后使用方便。将所有的这些知识,加以总结,以备后来人使用

热门栏目