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三层架构:持久层、业务层、表现层
    Spring:Spring JDBCTemplate & 声明式事务
    Spring:AOP
    Spring:IOC控制反转
    Mybatis: 加载策略及注解开发
    Mybatis 配置文件深入
    Mybatis:基本应用
    前后端项目接口联调
    springboot整合jsp
    IDEA出现URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
  • 原文地址:https://www.cnblogs.com/y279336671/p/2600820.html
Copyright © 2011-2022 走看看