zoukankan      html  css  js  c++  java
  • 黄聪: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, 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# for VS foreach 性能对比
    C# D3D中2D的使用,做小游戏。。。。半年前写的东西了,拿出来分享,现在看来代码写的乱七八糟的,将就一下吧。。。
    传奇3(G) 国际服 地图 显示 C#代码实现(地表草皮显示基本没有问题,但地面对象显示混乱)
    IE6和Opera position:absolute; 子元素浮动 width:100%;显示不正确问题。。。
    .NET Framework 4 文件IO读写的增强 激动人心的强大 或许正是你所期待的 基类库中的新增内容 转自msdn杂志
    VirtualBox 系统盘 虚拟磁盘 变大?
    假如你是ASP.NET 中手,个团队邀请你加入他们,没有工资(至少要等到项目卖出去,交付)[问题点数:100分] 创业号召贴,发帖保存
    对于C#的一些奢望(对微软的一种幻想,对ASP.NET,WEB,计算机,.NET,以及一些现状的抱怨)
    实例化 泛型 对象
    基于权值的微博用户采样算法研究
  • 原文地址:https://www.cnblogs.com/huangcong/p/1866559.html
Copyright © 2011-2022 走看看