如何编程替换文件夹中的word文档中的文字和图标

解决方案 »

  1.   

    800分求解决方案
    已经发了4个帖子 
    总共800分,那个给我个好的方案  分就全给他。
    http://topic.csdn.net/u/20110706/09/0ae09171-d68f-4a23-896b-c0140f2748f8.html
    http://topic.csdn.net/u/20110706/09/f329a05d-503a-4455-8769-65005cbc43c8.html
    http://topic.csdn.net/u/20110706/09/228fbfcf-b916-40e4-abe4-2ea2d308ca9c.html
    http://topic.csdn.net/u/20110706/09/524ab66a-d505-4931-aff5-f471b2f49466.html
      

  2.   

    2003 还是 2007(这个还好些 因为2007其实是一个zip 里面有xml文件 和资源文件)
      

  3.   

    http://www.jb51.net/article/17770.htm
    http://www.mscto.com/C__Net/27564203.html
      

  4.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using System.Reflection;
    using Microsoft.Office.Interop.Word;
    using System.Drawing;
    using System.IO;   /// <summary>
    /// OutputWod 的摘要说明
    /// </summary>
    public class OutputWod
    {    public OutputWod()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }    /// <summary>
        /// 替换word文档中的logo图片及其它  :替代前,需要在文档的logo创建 书签名:网站标志
        /// </summary>
        /// <param name="strName">线路名称</param>
        /// <param name="filePath">物理路径</param>
        /// <param name="filename">文档名称doc</param>
        /// <param name="filename">logo路径</param>  
        /// <returns></returns>
        public static void CreatWordDocument(string strName, string filePath, string filename, string pic)
        {      
            object Nothing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
            object Obj_FileName = filePath;
            object Visible = false;
            object ReadOnly = false;
            object missing = System.Reflection.Missing.Value;
            string strKey = "^p^p";      //要搜索的换行符  --参见code替代段 - 换行符的说明。
            try
            {
                //打开文件
                doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref Visible,
                    ref missing, ref missing, ref missing,
                    ref missing);
                doc.Activate();            //--判断logo图片是否存在·不存在则略过插入。
                if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(".." + pic)))
                {
                    foreach (Microsoft.Office.Interop.Word.Book bm in doc.Books)
                    {
                        if (bm.Name == "网站标志")
                        {
                            bm.Select();                        //要插入的图片文件
                            app.Selection.InlineShapes.AddPicture(System.Web.HttpContext.Current.Server.MapPath(".." + pic), ref missing, ref missing, ref missing);
                            //doc.Application.ActiveDocument.InlineShapes[1].Width = 100f; // 图片宽度 
                            //doc.Application.ActiveDocument.InlineShapes[1].Height = 8f; // 图片高度                        //将图片设置为四周环绕型
                            Microsoft.Office.Interop.Word.Shape cShape = doc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                            cShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTopBottom;
                            //cShape.Width = 8f;
                            //cShape.Height = 100f;
                            cShape.Left = -999998f;
                            cShape.Top = -999999f;// WdShapePosition.wdShapeCenter;居中
                            //cShape.WrapFormat.AllowOverlap = 0;
                            //cShape.LayoutInCell = 0;
                            //cShape.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
                            //cShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
                            //cShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;                        //设置图片浮于文字之上 - 查阅WdWrapType的相关WdWrapType Enumeration
                    cShape.WrapFormat.Type= WdWrapType.wdWrapNone;   
                    ////--图片载剪的高度及宽度
                    //cShape.PictureFormat.CropLeft = 2.8f;
                    //cShape.PictureFormat.CropRight = 2.8f;                ////--图片显示高比例和宽比例  -缩放比例
                    //cShape.ScaleHeight(1.00f, Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromMiddle);
                    //cShape.ScaleWidth(1.00f, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromMiddle);                         bm.Range.Text = "";
                            //doc.Content.Find.Text = strKey;  //调用查找 回车符标记
     
                            //清除原logo后,即时删除word文档已存在的回车符。
                            /*用替换法删除多余的空行 
                            如果是段落结束符产生的空行,可以在查找内容中输入^p^p,如果是换行符产生的空行,则在查找内容中输入^l^l。接着在替换为框中输入^p。 
                            解释:^p是段落标记的查找符,^l是换行符,即只进行换行,但未结束段落。*/
                            int i = 0, iCount = 0;
                           // Microsoft.Office.Interop.Word.Find wfnd;
                            //每次取一部分文档进行判断,符合条件就结束本次搜索,就可以避免每次搜索整个文档了。模型中的Paragraphs对象 可实现要求
                            if (doc.Paragraphs != null && doc.Paragraphs.Count > 5)
                            {
                                iCount = doc.Paragraphs.Count;
                                for (i = 1; i <= 5; i++)
                                {
                                    doc.Content.Find.Text = strKey;
                                    object FindText, ReplaceWith, Replace;// 
                                    object MissingValue = Type.Missing;
                                    FindText = strKey;//要查找的文本
                                    ReplaceWith = "^p";//替换文本
                                    Replace = Word.WdReplace.wdReplaceOne;
                                    /*wdReplaceAll - 替换找到的所有项。
                                     *wdReplaceNone - 不替换找到的任何项。
                                     * wdReplaceOne - 替换找到的第一项。*/
                                    doc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
                                    doc.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);
           
                                }                            
                            }
                            //结束退出
                            ////break;
                        }                 
                       
                    }
                }
                //注意,如果是打开一个HTML文档,而由HTML另存为doc文档时,插入的图片会生成ab.file这样的目录,来存储当前图片文件,如果是新建的空word文档,再把HTML导入到word文档中,再插入图片就不会出现ab.file这样的目录,图片会直接保存的word文档ab.doc中            object sdoc = System.Web.HttpContext.Current.Server.MapPath("../doc/" + strName + "_logo.doc");
                doc.SaveAs(ref sdoc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);            
            }
            catch(Exception mage)
            {
                //写入系统日志
                /*暂略*/
            }
            finally
            {            /*--- C# 读写Word :提示将 Word 用作自动化服务器时提示保存 Normal.dot (解决方案之1)   ----*/
                /////使用 Quit 方法的 SaveChanges 参数,如下所示: 
                ////object SaveOption, OriginalFormat, RouteDocument;
                ////SaveOption = Word.WdSaveOptions.wdPromptToSaveChanges;
                ////OriginalFormat = Word.WdOriginalFormat.wdPromptUser;
                ////RouteDocument = Missing.Value;
                ////if (wa != null)
                ////{
                ////    wa.Quit(ref SaveOption, ref OriginalFormat, ref RouteDocument);
                ////    wa = null;
                ////}            /*--- C# 读写Word :提示将 Word 用作自动化服务器时提示保存 Normal.dot (解决方案之2)   ----*/
                ///// wdApp2.Quit 方法的调用语句之前,添加以下行:
                app.NormalTemplate.Saved = true;
                //关闭网页doc
                doc.Close(ref Nothing, ref Nothing, ref Nothing);
                if (doc != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                    doc = null;
                }
                //关闭wordApp
                app.Quit(ref Nothing, ref Nothing, ref Nothing);
                if (app != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                    app = null;
                }
                //释放word进程
                GC.Collect();
            }
        }    ///<summary>
        /// 在word 中查找一个字符串直接替换所需要的文本
        /// </summary>
        /// <param name="strOldText">原文本</param>
        /// <param name="strNewText">新文本</param>
        /// <returns></returns>
        public static bool FindReplace(Microsoft.Office.Interop.Word.Document oDoc, string strOldText, string strNewText)
        {
            oDoc.Content.Find.Text = strOldText;
            object FindText, ReplaceWith, Replace;// 
            object MissingValue = Type.Missing;
            FindText = strOldText;//要查找的文本
            ReplaceWith = strNewText;//替换文本
            Replace = Word.WdReplace.wdReplaceAll;/**//*wdReplaceAll - 替换找到的所有项。
                                                          * wdReplaceNone - 不替换找到的任何项。
                                                        * wdReplaceOne - 替换找到的第一项。
                                                        * */
            oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
            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))
            {
                return true;
            }
            return false;    }    }
      

  5.   

    see~http://kb.cnblogs.com/a/1506242/
      

  6.   

    dsoframer  这个不错,可以通过js来操作
    简单写几个  网上有例子
    DSOFramer.ocx 控件使用  
    接口文档:/*
    1.新建
    */
    //新建Word
    document.all.FramerControl1.CreateNew("Word.Document");
    //新建Excel
    document.all.FramerControl1.CreateNew("Excel.Sheet");
    /*
    2.打开文件
    */
    //打开制定的本地文件
    document.all.FramerControl1.Open("C:\\TestBook.xls");
    //制定用Word来打开c:\plain.txt文件
    document.all.FramerControl1.Open("C:\\Plain.txt",false, "Word.Document");
    //打开服务器的文件 
    document.all.FramerControl1.Open "https://secureserver/test/mytest.asp?id=123",true, "Excel.Sheet", "MyUserAccount", "MyPassword");
    //打开服务器的文件 
    document.all.FramerControl1.Open("http://localhost/1.doc", true);
    /*
    3.保存文件
    */
    //到本地
    document.all.FramerControl1.Save("c:\\1.doc",true);
    //服务器   
    /*增加Http协议Post上传接口,可以Post一个动态页面(jsp,asp,php...),由动态页面负责解析数据
    bool HttpInit();
    bool HttpAddPostString(BSTR strName, BSTR strValue);
    bool HttpAddPostCurrFile(BSTR strFileID, BSTR strFileName);
    BSTR HttpPost(BSTR bstr);        
    */
    //初始化Http引擎
    document.all.FramerControl1.HttpInit();
    //增加Post变量
    document.all.FramerControl1.HttpAddPostString("RecordID","20060102200");
    document.all.FramerControl1.HttpAddPostString("UserID","李局长");
    //上传打开的文件
    document.all.FramerControl1.HttpAddPostCurrFile("FileData", "文档名.doc");
    //执行上传动作
    document.all.FramerControl1.HttpPost("http://xxxx.com/uploadfile.asp"); /*
    4.修订留痕
    */
    //进入留痕状态
    document.all.FramerControl1.SetTrackRevisions(1);
    //进入非留痕状态
    document.all.FramerControl1.SetTrackRevisions(0);
    //接受当前修订
    document.all.FramerControl1.SetTrackRevisions(4);
      

  7.   

    建设第三方控件,aspose.word
    要钱的。
      

  8.   

    HDNGO;zzs0618:
    还有如下一些问题
    1、你们给的程序 只能替换文档中的内容  而不能替换页眉的内容。
    2、页眉中的图标我想取消掉  因为公司的logo已经不用了。
    3、我想批量处理一个文件夹中的所有word文档,而不是一个一个的点替换。说明:公司里面的word文档非常多  我现在把那些文档全集中到一个文件夹里面了。差不多每个文档页眉都有"广东广州"4个字   而我要把他替换为"广东珠海"这4个字,而文档中也是替换这4个字。其他文字都不用替换的。还有就是页眉中有个logo我想删掉。而不是替换,也就是说页眉中不要图标了。帮我解决这些问题,我会马上结贴的。
    谢谢大家了!
      

  9.   

    HDNGO:
     在另外的帖子里面我已经给你分了,所以这个帖子我就不给你了,谢谢你