且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#实现WORD文档的内容复制和替换

更新时间:2022-09-09 12:59:37

最近一个项目的需求是要根据一个Word文档的模板,用记录集的具体内容替换掉里面的标识字符的内容,生成不同的文档。
分两步:
第一:复制模板的内容到一个Document对象里
C#实现WORD文档的内容复制和替换从源DOC文档复制内容返回一个Document类#region 从源DOC文档复制内容返回一个Document类 
C#实现WORD文档的内容复制和替换        /// <summary> 
C#实现WORD文档的内容复制和替换        /// 从源DOC文档复制内容返回一个Document类 
C#实现WORD文档的内容复制和替换        /// </summary> 
C#实现WORD文档的内容复制和替换        /// <param name="sorceDocPath">源DOC文档路径</param> 
C#实现WORD文档的内容复制和替换        /// <returns>Document</returns> 
C#实现WORD文档的内容复制和替换        protected Document copyWordDoc(object sorceDocPath)    
C#实现WORD文档的内容复制和替换        { 
C#实现WORD文档的内容复制和替换                object objDocType = WdDocumentType.wdTypeDocument; 
C#实现WORD文档的内容复制和替换                object type = WdBreakType.wdSectionBreakContinuous; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                //Word应用程序变量    
C#实现WORD文档的内容复制和替换                Application wordApp; 
C#实现WORD文档的内容复制和替换                //Word文档变量 
C#实现WORD文档的内容复制和替换                Document newWordDoc; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                object readOnly = false
C#实现WORD文档的内容复制和替换                object isVisible = false
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                //初始化 
C#实现WORD文档的内容复制和替换                //由于使用的是COM库,因此有许多变量需要用Missing.Value代替 
C#实现WORD文档的内容复制和替换                wordApp = new ApplicationClass(); 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                Object Nothing = System.Reflection.Missing.Value; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                //wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);     
C#实现WORD文档的内容复制和替换                newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                Document openWord; 
C#实现WORD文档的内容复制和替换                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); 
C#实现WORD文档的内容复制和替换                openWord.Select(); 
C#实现WORD文档的内容复制和替换                openWord.Sections[1].Range.Copy(); 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                object start = 0; 
C#实现WORD文档的内容复制和替换                Range newRang = newWordDoc.Range(ref start, ref start); 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                //插入换行符    
C#实现WORD文档的内容复制和替换                //newWordDoc.Sections[1].Range.InsertBreak(ref type); 
C#实现WORD文档的内容复制和替换                newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault); 
C#实现WORD文档的内容复制和替换                openWord.Close(ref Nothing, ref Nothing, ref Nothing); 
C#实现WORD文档的内容复制和替换                return newWordDoc; 
C#实现WORD文档的内容复制和替换        } 
C#实现WORD文档的内容复制和替换        #endregion
 
第二:替换复制好内容的Document的标识字符
C#实现WORD文档的内容复制和替换替换指定Document的内容,并保存到指定的路径#region 替换指定Document的内容,并保存到指定的路径 
C#实现WORD文档的内容复制和替换        /// <summary> 
C#实现WORD文档的内容复制和替换        /// 替换指定Document的内容,并保存到指定的路径 
C#实现WORD文档的内容复制和替换        /// </summary> 
C#实现WORD文档的内容复制和替换        /// <param name="docObject">Document</param> 
C#实现WORD文档的内容复制和替换        /// <param name="savePath">保存到指定的路径</param> 
C#实现WORD文档的内容复制和替换        protected void ReplaceWordDocAndSave(Document docObject, object savePath)    
C#实现WORD文档的内容复制和替换        { 
C#实现WORD文档的内容复制和替换                object format = WdSaveFormat.wdFormatDocument; 
C#实现WORD文档的内容复制和替换                object readOnly = false
C#实现WORD文档的内容复制和替换                object isVisible = false
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                string strOldText = "{WORD}"
C#实现WORD文档的内容复制和替换                string strNewText = "{替换后的文本}"
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                List<string> IListOldStr = new List<string>(); 
C#实现WORD文档的内容复制和替换                IListOldStr.Add("{WORD1}"); 
C#实现WORD文档的内容复制和替换                IListOldStr.Add("{WORD2}"); 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                Object Nothing = System.Reflection.Missing.Value; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass(); 
C#实现WORD文档的内容复制和替换                //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); 
C#实现WORD文档的内容复制和替换                Microsoft.Office.Interop.Word.Document oDoc = docObject; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                object FindText, ReplaceWith, Replace; 
C#实现WORD文档的内容复制和替换                object MissingValue = Type.Missing; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                foreach (string str in IListOldStr) 
C#实现WORD文档的内容复制和替换                { 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                        oDoc.Content.Find.Text = str; 
C#实现WORD文档的内容复制和替换                        //要查找的文本 
C#实现WORD文档的内容复制和替换                        FindText = str; 
C#实现WORD文档的内容复制和替换                        //替换文本 
C#实现WORD文档的内容复制和替换                        ReplaceWith = strNewText; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                        //wdReplaceAll - 替换找到的所有项。 
C#实现WORD文档的内容复制和替换                        //wdReplaceNone - 不替换找到的任何项。 
C#实现WORD文档的内容复制和替换                        //wdReplaceOne - 替换找到的第一项。 
C#实现WORD文档的内容复制和替换                        Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                        //移除Find的搜索文本和段落格式设置 
C#实现WORD文档的内容复制和替换                        oDoc.Content.Find.ClearFormatting(); 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                        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)) 
C#实现WORD文档的内容复制和替换                        { 
C#实现WORD文档的内容复制和替换                                Response.Write("替换成功!"); 
C#实现WORD文档的内容复制和替换                                Response.Write("<br>"); 
C#实现WORD文档的内容复制和替换                        } 
C#实现WORD文档的内容复制和替换                        else 
C#实现WORD文档的内容复制和替换                        { 
C#实现WORD文档的内容复制和替换                                Response.Write("没有相关要替换的:(" + str + ")字符"); 
C#实现WORD文档的内容复制和替换                                Response.Write("<br>"); 
C#实现WORD文档的内容复制和替换                        } 
C#实现WORD文档的内容复制和替换                } 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                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); 
C#实现WORD文档的内容复制和替换 
C#实现WORD文档的内容复制和替换                //关闭wordDoc文档对象     
C#实现WORD文档的内容复制和替换                oDoc.Close(ref Nothing, ref Nothing, ref Nothing); 
C#实现WORD文档的内容复制和替换                //关闭wordApp组件对象     
C#实现WORD文档的内容复制和替换                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); 
C#实现WORD文档的内容复制和替换        } 
C#实现WORD文档的内容复制和替换        #endregion
 
用到了C#操作WORD的复制,替换,和创建WORD文档的知识。



本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/archive/2010/11/01/1866559.html,如需转载请自行联系原作者