///從源DOC文檔復(fù)制內(nèi)容返回一個(gè)Document類//////源DOC文檔路徑

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

黃聰:C#實(shí)現(xiàn)WORD文檔的內(nèi)容復(fù)制和替換

系統(tǒng) 2348 0
最近一個(gè)項(xiàng)目的需求是要根據(jù)一個(gè)Word文檔的模板,用記錄集的具體內(nèi)容替換掉里面的標(biāo)識(shí)字符的內(nèi)容,生成不同的文檔。
分兩步:
第一:復(fù)制模板的內(nèi)容到一個(gè)Document對(duì)象里
從源DOC文檔復(fù)制內(nèi)容返回一個(gè)Document類 #region 從源DOC文檔復(fù)制內(nèi)容返回一個(gè)Document類
???????? /// <summary>
???????? /// 從源DOC文檔復(fù)制內(nèi)容返回一個(gè)Document類
???????? /// </summary>
???????? /// <param name= "sorceDocPath" > 源DOC文檔路徑 </param>
???????? /// <returns> Document </returns>
???????? protected Document copyWordDoc( object sorceDocPath)????
????????{
???????????????? object objDocType = WdDocumentType.wdTypeDocument;
???????????????? object type = WdBreakType.wdSectionBreakContinuous;

???????????????? //Word應(yīng)用程序變量????
????????????????Application wordApp;
???????????????? //Word文檔變量
????????????????Document newWordDoc;

???????????????? object readOnly = false ;
???????????????? object isVisible = false ;

???????????????? //初始化
???????????????? //由于使用的是COM庫,因此有許多變量需要用Missing.Value代替
????????????????wordApp = new ApplicationClass();

????????????????Object Nothing = System.Reflection.Missing.Value;

???????????????? //wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);????
????????????????newWordDoc = wordApp.Documents.Add( ref Nothing, ref Nothing, ref Nothing, ref Nothing);

????????????????Document openWord;
????????????????openWord = wordApp.Documents.Open( ref sorceDocPath, 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);
????????????????openWord.Select();
????????????????openWord.Sections[1].Range.Copy();

???????????????? object start = 0;
????????????????Range newRang = newWordDoc.Range( ref start, ref start);

???????????????? //插入換行符????
???????????????? //newWordDoc.Sections[1].Range.InsertBreak(ref type);
????????????????newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
????????????????openWord.Close( ref Nothing, ref Nothing, ref Nothing);
???????????????? return newWordDoc;
????????}
????????#endregion
?
第二:替換復(fù)制好內(nèi)容的Document的標(biāo)識(shí)字符
替換指定Document的內(nèi)容,并保存到指定的路徑 #region 替換指定Document的內(nèi)容,并保存到指定的路徑
???????? /// <summary>
???????? /// 替換指定Document的內(nèi)容,并保存到指定的路徑
???????? /// </summary>
???????? /// <param name= "docObject" > Document </param>
???????? /// <param name= "savePath" > 保存到指定的路徑 </param>
???????? protected void ReplaceWordDocAndSave(Document docObject, object savePath)????
????????{
???????????????? object format = WdSaveFormat.wdFormatDocument;
???????????????? object readOnly = false ;
???????????????? object isVisible = false ;

???????????????? string strOldText = "{WORD}" ;
???????????????? string strNewText = "{替換后的文本}" ;

????????????????List< string > IListOldStr = new List< string >();
????????????????IListOldStr.Add( "{WORD1}" );
????????????????IListOldStr.Add( "{WORD2}" );

????????????????Object Nothing = System.Reflection.Missing.Value;

????????????????Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass();
???????????????? //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);
????????????????Microsoft.Office.Interop.Word.Document oDoc = docObject;

???????????????? object FindText, ReplaceWith, Replace;
???????????????? object MissingValue = Type.Missing;

???????????????? foreach ( string str in IListOldStr)
????????????????{

????????????????????????oDoc.Content.Find.Text = str;
???????????????????????? //要查找的文本
????????????????????????FindText = str;
???????????????????????? //替換文本
????????????????????????ReplaceWith = strNewText;

???????????????????????? //wdReplaceAll - 替換找到的所有項(xiàng)。
???????????????????????? //wdReplaceNone - 不替換找到的任何項(xiàng)。
???????????????????????? //wdReplaceOne - 替換找到的第一項(xiàng)。
????????????????????????Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

???????????????????????? //移除Find的搜索文本和段落格式設(shè)置
????????????????????????oDoc.Content.Find.ClearFormatting();

???????????????????????? if (oDoc.Content.Find.Execute( ref FindText, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref ReplaceWith, ref Replace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue))
????????????????????????{
????????????????????????????????Response.Write( "替換成功!" );
????????????????????????????????Response.Write( "<br>" );
????????????????????????}
???????????????????????? else
????????????????????????{
????????????????????????????????Response.Write( "沒有相關(guān)要替換的:(" + str + ")字符" );
????????????????????????????????Response.Write( "<br>" );
????????????????????????}
????????????????}

????????????????oDoc.SaveAs( ref savePath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

???????????????? //關(guān)閉wordDoc文檔對(duì)象????
????????????????oDoc.Close( ref Nothing, ref Nothing, ref Nothing);
???????????????? //關(guān)閉wordApp組件對(duì)象????
????????????????wordApp.Quit( ref Nothing, ref Nothing, ref Nothing);
????????}
????????#endregion
?
用到了C#操作WORD的復(fù)制,替換,和創(chuàng)建WORD文檔的知識(shí)。

本文出自 “ 搖滾互聯(lián)網(wǎng) ” 博客,請(qǐng)務(wù)必保留此出處 http://tobetobe.blog.51cto.com/1392243/354420

黃聰:C#實(shí)現(xiàn)WORD文檔的內(nèi)容復(fù)制和替換


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 一区二区三区视频免费观看 | 九九热爱视频精品视频高清 | 精品一区二区三区免费 | 国产日韩欧美 | √新版天堂资源在线资源 | 欧美在线国产 | 亚洲精选一区 | 亚洲热色| 激情小说五月 | 亚洲精品第一国产综合野 | 国产一区 | 一区二区精品 | 国产色婷婷亚洲99精品小说 | 1234成人网站 | 国产欧美一区二区精品久久久 | 欧美一级α | 欧美一级美国一级 | 老司机精品视频个人在观看 | 国产99视频在线 | 亚洲九九色 | 日韩在线精品 | 久久久7777888精品 | 久久久久九九九九 | 国产人A片777777久久 | 日本久久黄色 | 国产精品成在线观看 | 三级三级三级a三级三级 | 精品久久一二三区 | 日本一区二区不卡 | 91免费版在线观看 | 91麻豆精品久久久久蜜臀 | 国产精品综合色区小说 | 色站综合 | 国产精品久久自在自2021 | 精品国产成人在线 | 丝袜美腿精品一区二区三 | 国产乱码精品一区二区三上 | 久久久久久久一区二区 | 久久久精 | av在线播放免费 | 乳欲人妻办公室奶水在线电影国产 |