C# 利用占位符替換word中的字符串和添加圖片


利用占位符替換word中的字符串和添加圖片
  /// <summary>
        /// 替換word模板文件內容,包括表格中內容
        /// 調用如下:WordStringsReplace("D:/CNSI/CNSI_1.doc", new ArrayList() { "old1", "old2" }, new ArrayList() { "new1", "new2" });
        /// </summary>
        /// <param name="filePath"> 文件全路徑 </param>
        /// <param name="arr_Old"> 占位符數組 </param>
        /// <param name="arr_New"> 替換字符串數組 </param>
        public void WordStringsReplace( string filePath, ArrayList arr_Old, ArrayList arr_New)
        {
            if (! File .Exists(filePath))
            {
                MessageBox .Show( "模板文件不存在!" );
                return ;
            }
            if (arr_Old.Count != arr_New.Count)
            {
                MessageBox .Show( "占位符和替換內容不一致!" );
                return ;
            }
            Microsoft.Office.Interop.Word. Application app = new Microsoft.Office.Interop.Word. Application ();
            object oMissing = System.Reflection. Missing .Value;
            object file = filePath;
            Microsoft.Office.Interop.Word. _Document doc = app.Documents.Open( ref file,
                   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            for ( int i = 0; i < arr_Old.Count; i++)
            {
                app.Selection.Find.ClearFormatting();
                app.Selection.Find.Replacement.ClearFormatting();
                app.Selection.Find.Text = arr_Old[i].ToString();
                app.Selection.Find.Replacement.Text = arr_New[i].ToString();
                object objReplace = Microsoft.Office.Interop.Word. WdReplace .wdReplaceAll;
                app.Selection.Find.Execute( ref oMissing, ref oMissing, ref oMissing,
                                           ref oMissing, ref oMissing, ref oMissing,
                                           ref oMissing, ref oMissing, ref oMissing,
                                           ref oMissing, ref objReplace, ref oMissing,
                                           ref oMissing, ref oMissing, ref oMissing);
            }
        
            //保存
            doc.Save();
            doc.Close( ref oMissing, ref oMissing, ref oMissing);
            app.Quit( ref oMissing, ref oMissing, ref oMissing);
        }
        /// <summary>
        /// 替換word模板文件中圖片,這個只能替換一個圖片,多個測試有點問題
        /// </summary>
        /// <param name="filePath"> 文件全路徑 </param>
        /// <param name="str_Old"> 占位符字符串 </param>
        /// <param name="str_Pics"> 替換圖片路徑 </param>
        /// <param name="x"> x點位置,(0,0)在該占位符所在行和列的原點 </param>
        /// <param name="y"> y點位置 </param>
        /// <param name="width"> 圖片寬度 </param>
        /// <param name="height"> 圖片高度 </param>
        public void WordStringsReplace( string filePath, String str_Old, String str_Pics, int x, int y, int w, int h)
        {
            if (! File .Exists(filePath))
            {
                MessageBox .Show( "模板文件不存在!" );
                return ;
            }
            Microsoft.Office.Interop.Word. Application app = new Microsoft.Office.Interop.Word. Application ();
            object oMissing = System.Reflection. Missing .Value;
            object file = filePath;
            Microsoft.Office.Interop.Word. _Document doc = app.Documents.Open( ref file,
                   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                   ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            object replaceAll = Microsoft.Office.Interop.Word. WdReplace .wdReplaceAll;
            object LinkToFile = false ;
            object SaveWithDocument = true ;
            app.Selection.Find.ClearFormatting();
            app.Selection.Find.Text = str_Old;
            app.Selection.Find.Execute( ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            object Anchor = app.Selection.Range;
            //oDoc.InlineShapes.AddPicture(picfileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);         
            object left = x;
            object top = y;
            object width = w;
            object height = h;
            Anchor = app.Selection.Range;
            doc.Shapes.AddPicture(str_Pics, ref LinkToFile, ref SaveWithDocument, ref left, ref top, ref width, ref height, ref   Anchor);
            //保存
            doc.Save();
            doc.Close( ref oMissing, ref oMissing, ref oMissing);
            app.Quit( ref oMissing, ref oMissing, ref oMissing);
            //清除占位符
            WordStringsReplace( "D:/CNSI/CNSI_1.doc" , new ArrayList () { str_Old }, new ArrayList () { " " });
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM