当前位置:软件学习 > Word >>

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

最近一个项目的需求是要根据一个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,
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,