///從源DOC文檔復制內容返回一個Document類//////源DOC文檔路徑

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

黃聰:C#實現WORD文檔的內容復制和替換

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

???????????????? //Word應用程序變量????
????????????????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
?
第二:替換復制好內容的Document的標識字符
替換指定Document的內容,并保存到指定的路徑 #region 替換指定Document的內容,并保存到指定的路徑
???????? /// <summary>
???????? /// 替換指定Document的內容,并保存到指定的路徑
???????? /// </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 - 替換找到的所有項。
???????????????????????? //wdReplaceNone - 不替換找到的任何項。
???????????????????????? //wdReplaceOne - 替換找到的第一項。
????????????????????????Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

???????????????????????? //移除Find的搜索文本和段落格式設置
????????????????????????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( "沒有相關要替換的:(" + 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);

???????????????? //關閉wordDoc文檔對象????
????????????????oDoc.Close( ref Nothing, ref Nothing, ref Nothing);
???????????????? //關閉wordApp組件對象????
????????????????wordApp.Quit( ref Nothing, ref Nothing, ref Nothing);
????????}
????????#endregion
?
用到了C#操作WORD的復制,替換,和創建WORD文檔的知識。

本文出自 “ 搖滾互聯網 ” 博客,請務必保留此出處 http://tobetobe.blog.51cto.com/1392243/354420

黃聰:C#實現WORD文檔的內容復制和替換


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人精品免费视频大全五级 | 久久精品国产亚洲黑森林 | 欧美交换乱理伦片在线观看 | 国内精品久久久久久中文字幕 | 一本大道香蕉大vr在线吗视频 | 欧美伊人 | 亚洲aa| 国产不卡在线观看视频 | 日韩中文精品亚洲第三区 | 日韩精品中文字幕视频一区 | 色九九 | 中文一级黄色片 | 欧美一区二区三 | 久操这里只有精品 | 四虎国产永久在线精品免费观看 | 欧美精品一区二区精品久久 | 尹人香蕉网在线观看视频 | 普通话对白国产情侣自啪 | 成人午夜啪啪免费网站 | 国产免费专区 | 亚洲一区欧洲一区 | 亚洲国产成人久久 | 麻豆一区二区 | 中文乱码在线观看 | 亚洲精品一区二区三区香蕉在线看 | 干欧美女人 | 看全色黄大色黄大片色责看的 | 久久国产经典视频 | 亚洲在线小视频 | 99re热视频精品首页 | 国内永久第一免费福利视频 | 天天曰天天干天天操 | 四虎永久在线精品视频免费观看 | 国产精品极品 | 亚洲精品成人a | 亚洲天码中文字幕第一页 | 国产精品午夜高清在线观看 | 日韩美女视频一区 | 久久综合久久精品 | 看欧美毛片一级毛片 | 全免费a级毛片免费看视频免 |