Word复制和替换实例

系统 1883 0
      
        public
      
      
        string
      
      
         Path

        {

            
      
      
        get
      
      
        

            {

                DirectoryInfo info 
      
      = 
      
        new
      
      
         DirectoryInfo(Application.StartupPath);

                
      
      
        return
      
       info.Parent.Parent.FullName + 
      
        "
      
      
        \\WordData\\Table.docx
      
      
        "
      
      
        ;

            }

        }

        
      
      
        //
      
      
        【C# 在word文档中复制表格并粘帖到下一页中】
      
      
        private
      
      
        void
      
       button1_Click(
      
        object
      
      
         sender, EventArgs e)

        {

            
      
      
        object
      
       missing =
      
         Missing.Value;

            Word.Application app 
      
      = 
      
        new
      
      
         Word.Application();

            app.Visible 
      
      = 
      
        true
      
      
        ;

            
      
      
        //
      
      
        导入模板
      
      
        object
      
       filename =
      
         Path;

            Word.Document doc 
      
      = app.Documents.Add(
      
        ref
      
      
         filename, missing, missing, missing);



            
      
      
        //
      
      
        复制第一个表格
      
      

            doc.Tables[
      
        1
      
      
        ].Select();

            app.Selection.Copy();

            
      
      
        //
      
      
        在这里操作表格的文本
      
      

            Word.Cell cellOne = doc.Tables[
      
        1
      
      ].Cell(
      
        1
      
      , 
      
        1
      
      
        );

            cellOne.Range.Text 
      
      = 
      
        "
      
      
        这是第一个表格
      
      
        "
      
      
        ;

            cellOne.Range.Bold 
      
      = 
      
        2
      
      
        ;

            cellOne.Range.Font.ColorIndex 
      
      =
      
         Word.WdColorIndex.wdRed;







            
      
      
        //
      
      
        下一页
      
      
        object
      
       myunit =
      
         Word.WdUnits.wdStory;

            app.Selection.EndKey(
      
      
        ref
      
       myunit, 
      
        ref
      
      
         missing);

            
      
      
        object
      
       pBreak = (
      
        int
      
      
        )Word.WdBreakType.wdPageBreak;

            app.Selection.InsertBreak(
      
      
        ref
      
      
         pBreak);



            
      
      
        //
      
      
        粘贴第一个表格
      
      
                    app.Selection.Paste();



            
      
      
        //
      
      
        操作第二个表格单元格
      
      

            Word.Cell cellTwo = doc.Tables[
      
        2
      
      ].Cell(
      
        1
      
      , 
      
        1
      
      
        );

            cellTwo.Range.Text 
      
      = 
      
        "
      
      
        这是第二个表格
      
      
        "
      
      
        ;

            cellTwo.Range.Underline 
      
      =
      
         Word.WdUnderline.wdUnderlineDash;

        }



        
      
      
        //
      
      
        【C#实现WORD文档的内容复制和替换】
      
      
        private
      
      
        void
      
       button2_Click(
      
        object
      
      
         sender, EventArgs e)

        {

            LocalPathHelper pathHelper 
      
      = 
      
        new
      
      
         LocalPathHelper();

            
      
      
        string
      
       sourceWord = WordPath.GetWordDataFullFileName(
      
        "
      
      
        copy.docx
      
      
        "
      
      
        );

            
      
      
        string
      
       targetWord = pathHelper.DesktopPath() + 
      
        "
      
      
        \\target.docx
      
      
        "
      
      
        ;

            
      
      
        //
      
      
        复制文件
      
      

            Word.Document doc =
      
         copyWord(sourceWord);

            
      
      
        //
      
      
        查找替换
      
      
        

            ReplaceAndSave(doc, targetWord);

        }



        
      
      
        //
      
      
        复制word内容到Document对象
      
      
        public
      
       Word.Document copyWord(
      
        object
      
      
         sourcePath)

        {

            
      
      
        object
      
       objDocType =
      
         Word.WdDocumentType.wdTypeDocument;

            
      
      
        object
      
       type =
      
         Word.WdBreakType.wdSectionBreakContinuous;

            
      
      
        object
      
       missing =
      
         Missing.Value;



            Word.Application app 
      
      = 
      
        new
      
      
         Word.Application();

            Word.Document doc;



            
      
      
        object
      
       readOnly = 
      
        false
      
      
        ;

            
      
      
        object
      
       isVisible = 
      
        false
      
      
        ;



            doc 
      
      = app.Documents.Add(
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
      
         missing);

            Word.Document opendWord 
      
      = app.Documents.Open(
      
        ref
      
       sourcePath, 
      
        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);

            opendWord.Select();

            opendWord.Sections[
      
      
        1
      
      
        ].Range.Copy();



            
      
      
        object
      
       start = 
      
        0
      
      
        ;

            Word.Range newRange 
      
      = doc.Range(
      
        ref
      
       start, 
      
        ref
      
      
         start);



            
      
      
        //
      
      
        插入换行符

            
      
      
        //
      
      
        newWordDoc.Sections[1].Range.InsertBreak(ref type); 
      
      

            doc.Sections[
      
        1
      
      
        ].Range.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);

            opendWord.Close(
      
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
      
         missing);

            
      
      
        return
      
      
         doc;

        }



        
      
      
        //
      
      
        替换复制好的内容
      
      
        public
      
      
        void
      
       ReplaceAndSave(Word.Document doc, 
      
        object
      
      
         savePath)

        {

            
      
      
        object
      
       format =
      
         Word.WdSaveFormat.wdFormatDocument;

            
      
      
        object
      
       missing =
      
         Missing.Value;

            
      
      
        object
      
       readOnly = 
      
        false
      
      
        ;

            
      
      
        object
      
       isVisible = 
      
        false
      
      
        ;



            
      
      
        string
      
       strOldText = 
      
        "
      
      
        {Word}
      
      
        "
      
      
        ;

            
      
      
        string
      
       strNewText = 
      
        "
      
      
        {提花后的文本}
      
      
        "
      
      
        ;

            List
      
      <
      
        string
      
      > listStr = 
      
        new
      
       List<
      
        string
      
      >
      
        ();

            listStr.Add(
      
      
        "
      
      
        {Word1}
      
      
        "
      
      
        );

            listStr.Add(
      
      
        "
      
      
        {Word2}
      
      
        "
      
      
        );



            Word.Application app 
      
      = 
      
        new
      
      
         Word.Application();

            
      
      
        //
      
      
        Microsoft.Office.Interop.Word.Document oDoc = wordApp.Documents.Open(ref obj, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing); 
      
      

            Word.Document newDoc =
      
         doc;



            
      
      
        object
      
      
         FindText, ReplaceText, ReplaceAll;

            
      
      
        foreach
      
       (
      
        string
      
       str 
      
        in
      
      
         listStr)

            {

                newDoc.Content.Find.Text 
      
      =
      
         str;

                
      
      
        //
      
      
        要找的文本
      
      

                FindText =
      
         str;

                
      
      
        //
      
      
        替换文本
      
      

                ReplaceText =
      
         strNewText;

                
      
      
        //
      
      
        wdReplaceAll - 替换找到的所有项。 

                
      
      
        //
      
      
        wdReplaceNone - 不替换找到的任何项。 

                
      
      
        //
      
      
        wdReplaceOne - 替换找到的第一项。 
      
      

                ReplaceAll =
      
         Word.WdReplace.wdReplaceAll;

                
      
      
        //
      
      
        移除Find的搜索文本和段落格式设置
      
      
                        newDoc.Content.Find.ClearFormatting();



                
      
      
        if
      
       (newDoc.Content.Find.Execute(
      
        ref
      
       FindText, 
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
      
         missing,

                    
      
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
       ReplaceText, 
      
        ref
      
       ReplaceAll, 
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
      
         missing))

                {

                    MessageBox.Show(
      
      
        "
      
      
        替换成功
      
      
        "
      
      
        );

                }

                
      
      
        else
      
      
        

                {

                    MessageBox.Show(
      
      
        "
      
      
        没有相关替换的:'
      
      
        "
      
       + str + 
      
        "
      
      
        '字符
      
      
        "
      
      
        );

                }

            }



            newDoc.SaveAs2(
      
      
        ref
      
      
         savePath);

            
      
      
        //
      
      
        关闭文档对象,关闭组件对象
      
      

            newDoc.Close(
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
      
         missing);

            app.Quit(
      
      
        ref
      
       missing, 
      
        ref
      
       missing, 
      
        ref
      
      
         missing);

        }
      
    

 

Word复制和替换实例


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论