zoukankan      html  css  js  c++  java
  • word替换字符串为文本或图片

    using System;
    using System.IO;
    using Word = Microsoft.Office.Interop.Word;
     
     
    namespace SavePicture
    {
        public class CreateTemplate
        {
            /// <summary>
            /// 替换成指定字符串
            /// </summary>
            /// <param name="wordPath">要替换的word全路径</param>
            /// <param name="oldText">替换的文字</param>
            /// <param name="newText">替换成的文字</param>
            /// <returns>是否替换成功</returns>
            public static bool ReplaceWord(string wordPath, string oldText, string newText)
            {
                try
                {
                    object nothing = System.Reflection.Missing.Value;
                    object format = Word.WdSaveFormat.wdFormatDocument;
                    object srcFileName = wordPath;
                    object wrap = Word.WdFindWrap.wdFindContinue;
     
                    Word.ApplicationClass appWord = new Word.ApplicationClass();
                    Word.Document wordDocument = appWord.Documents.Open(ref srcFileName, ref format, 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);
                    appWord.Selection.Find.ClearFormatting();
     
                    appWord.Selection.Find.Replacement.ClearFormatting();
     
     
     
                    object objReplace = Word.WdReplace.wdReplaceAll;
                    appWord.Selection.Find.ClearFormatting();
                    appWord.Selection.Find.Replacement.ClearFormatting();
     
     
                    appWord.Selection.Find.Text = oldText;
                    appWord.Selection.Find.Replacement.Text = newText;
                    appWord.Selection.Find.Execute(ref nothing, ref nothing, ref nothing,
                                                   ref nothing, ref nothing, ref nothing,
                                                   ref nothing, ref wrap, ref nothing,
                                                   ref nothing, ref objReplace, ref nothing,
                                                   ref nothing, ref nothing, ref nothing);
     
     
                    wordDocument.Save();
                    wordDocument.Close();
                    return true;
                }
                catch (Exception)
                {
                    return false;
     
                }
            }
     
            /// <summary>
            /// 替换word中指定文本为图片
            /// </summary>
            /// <param name="wordPath">word全路径</param>
            /// <param name="textLabel">替换字符串</param>
            /// <param name="imagePath">图片全路径</param>
            /// <returns>是否替换成功</returns>
            public static bool ReplaceImage(string wordPath, string textLabel, string imagePath)
            {
                try
                {
                    object nothing = System.Reflection.Missing.Value;
                    object missing = Type.Missing;
                    object SaveWithDocument = true;
                    object LinkToFile = false;
                    object format = Word.WdSaveFormat.wdFormatDocument;
                    object link = false;
                    object confirmConversion = false;
                    object objReplace = Word.WdReplace.wdReplaceOne;
                    object wrap = Word.WdFindWrap.wdFindContinue;
                    object srcFileName = wordPath;
     
                    Word.ApplicationClass appWord = new Word.ApplicationClass();
                    Word.Document wordDocument = appWord.Documents.Open(ref srcFileName, ref format, 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);
                    //替换图片过程
                 
                    appWord.Selection.Find.ClearFormatting();
                    appWord.Selection.Find.Replacement.ClearFormatting();
                    bool found;
                    appWord.Selection.Find.Text = textLabel;
                    appWord.Selection.Find.Replacement.Text = "";
     
                    if (File.Exists(imagePath))
                    {
                        //替换成图片
                        do
                        {
                            found = appWord.Selection.Find.Execute(ref nothing, ref nothing, ref nothing,
                                                                   ref nothing, ref nothing, ref nothing,
                                                                   ref nothing, ref wrap, ref nothing,
                                                                   ref nothing, ref objReplace, ref nothing,
                                                                   ref nothing, ref nothing, ref nothing);
                            if (found)
                            {
                                appWord.Selection.InlineShapes.AddPicture(imagePath, ref LinkToFile, ref SaveWithDocument, ref missing);
                            }
                        } while (appWord.Selection.Find.Found == true);
                    }
                    wordDocument.Save();
                    wordDocument.Close();
                    return true;
                }
                catch (Exception)
                {
                    return false;
     
                }
            }
     
     
     
        }
    }
  • 相关阅读:
    JAVA BASE64
    Mysql Index extends优化
    JAVA异步加回调的例子
    TCP长连接和短连接的区别
    浅析RPC概念框架
    MySQL Index Merge Optimization
    一简单的RPC实例(Java)
    return语句的用法
    java中对象和对象的引用
    UML中几种类间关系:继承、实现、依赖、关联、聚合、组合的联系与区别
  • 原文地址:https://www.cnblogs.com/y279336671/p/2600820.html
Copyright © 2011-2022 走看看