最近一個項目的需求是要根據一個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的内容,并儲存到指定的路徑 /// 替換指定Document的内容,并儲存到指定的路徑 /// <param name="docObject">Document</param> /// <param name="savePath">儲存到指定的路徑</param> protected void ReplaceWordDocAndSave(Document docObject, object savePath) object format = WdSaveFormat.wdFormatDocument; string strOldText = "{WORD}"; string strNewText = "{替換後的文本}"; List<string> IListOldStr = new List<string>(); IListOldStr.Add("{WORD1}"); IListOldStr.Add("{WORD2}"); 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, refMissingValue, ref MissingValue, ref ReplaceWith, ref Replace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue)) { Response.Write("替換成功!"); Response.Write("<br>"); } else Response.Write("沒有相關要替換的:(" + str + ")字元"); } oDoc.SaveAs(ref savePath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, refNothing, 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); 用到了C#操作WORD的複制,替換,和建立WORD文檔的知識。 本文轉自黃聰部落格園部落格,原文連結:http://www.cnblogs.com/huangcong/archive/2010/11/01/1866559.html,如需轉載請自行聯系原作者