亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Word復(fù)制和替換實(shí)例

系統(tǒng) 2069 0
      
        public
      
      
        string
      
      
         Path

        {

            
      
      
        get
      
      
        

            {

                DirectoryInfo info 
      
      = 
      
        new
      
      
         DirectoryInfo(Application.StartupPath);

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

            }

        }

        
      
      
        //
      
      
        【C# 在word文檔中復(fù)制表格并粘帖到下一頁中】
      
      
        private
      
      
        void
      
       button1_Click(
      
        object
      
      
         sender, EventArgs e)

        {

            
      
      
        object
      
       missing =
      
         Missing.Value;

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

            app.Visible 
      
      = 
      
        true
      
      
        ;

            
      
      
        //
      
      
        導(dǎo)入模板
      
      
        object
      
       filename =
      
         Path;

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



            
      
      
        //
      
      
        復(fù)制第一個(gè)表格
      
      

            doc.Tables[
      
        1
      
      
        ].Select();

            app.Selection.Copy();

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

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

            cellOne.Range.Text 
      
      = 
      
        "
      
      
        這是第一個(gè)表格
      
      
        "
      
      
        ;

            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);



            
      
      
        //
      
      
        粘貼第一個(gè)表格
      
      
                    app.Selection.Paste();



            
      
      
        //
      
      
        操作第二個(gè)表格單元格
      
      

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

            cellTwo.Range.Text 
      
      = 
      
        "
      
      
        這是第二個(gè)表格
      
      
        "
      
      
        ;

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

        }



        
      
      
        //
      
      
        【C#實(shí)現(xiàn)WORD文檔的內(nèi)容復(fù)制和替換】
      
      
        private
      
      
        void
      
       button2_Click(
      
        object
      
      
         sender, EventArgs e)

        {

            LocalPathHelper pathHelper 
      
      = 
      
        new
      
      
         LocalPathHelper();

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

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

            
      
      
        //
      
      
        復(fù)制文件
      
      

            Word.Document doc =
      
         copyWord(sourceWord);

            
      
      
        //
      
      
        查找替換
      
      
        

            ReplaceAndSave(doc, targetWord);

        }



        
      
      
        //
      
      
        復(fù)制word內(nèi)容到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;

        }



        
      
      
        //
      
      
        替換復(fù)制好的內(nèi)容
      
      
        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 - 替換找到的所有項(xiàng)。 

                
      
      
        //
      
      
        wdReplaceNone - 不替換找到的任何項(xiàng)。 

                
      
      
        //
      
      
        wdReplaceOne - 替換找到的第一項(xiàng)。 
      
      

                ReplaceAll =
      
         Word.WdReplace.wdReplaceAll;

                
      
      
        //
      
      
        移除Find的搜索文本和段落格式設(shè)置
      
      
                        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(
      
      
        "
      
      
        沒有相關(guān)替換的:'
      
      
        "
      
       + str + 
      
        "
      
      
        '字符
      
      
        "
      
      
        );

                }

            }



            newDoc.SaveAs2(
      
      
        ref
      
      
         savePath);

            
      
      
        //
      
      
        關(guān)閉文檔對象,關(guān)閉組件對象
      
      

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

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

        }
      
    

?

Word復(fù)制和替換實(shí)例


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 人人干天天干 | 四虎精品免费永久在线 | 色婷婷亚洲综合 | 欧美一级毛片不卡免费观看 | 国产精品你懂的在线播放 | 色综合久久精品中文字幕首页 | 欧美一级看片a免费视频 | 九九视频在线观看视频6偷拍 | 青青爽国产手机在线观看免费 | 国产精品毛片久久久久久久 | 中文字幕一二三区 | 亚洲精品福利在线观看 | 日韩欧美视频免费观看 | 国产精品1区2区3区在线播放 | 日本在线 | 中文 | 色一情| 99热久久精品国产 | 免费在线一区二区三区 | 日韩欧美福利 | 黄色毛片视频免费 | 亚洲视频在线看 | 欧美日韩一区二区三区麻豆 | 一级特黄aa大片欧美小说 | 五月婷婷免费视频 | 国产精品久久久久久久福利院 | 亚洲国产成人精品久久 | 最新狠狠色狠狠色综合 | 成人性色大片 | 久久久久久免费视频 | 亚洲日本一区二区三区在线不卡 | 亚洲第五色综合网 | 中文字幕国产在线观看 | 苗族一级特黄a大片 | 热久久久| 在线视频一区二区日韩国产 | 久久久久久a亚洲欧洲aⅴ | 亚洲人人精品 | 免费可以直接观看的毛片 | jizz中国zz女女18| 久久久成人影院 | 九九香蕉网 |