• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
利用占位符替换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() { " " });
        }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中读取文本文件导入SQL数据库解决方法发布时间:2022-07-10
下一篇:
短信任务源程序分享[.NETCFC#]发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap