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

asp.net操作word模板

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

主要有两个方法

/// <summary>
        /// 从源DOC文档复制内容返回一个Document类
        /// </summary>
        /// <param name="sorceDocPath">源DOC文档路径</param>
        /// <returns>Document</returns>
        protected Document copyWordDoc(object sorceDocPath)
        {
            object objDocType = WdDocumentType.wdTypeDocument;
            object type = WdBreakType.wdSectionBreakContinuous;

            //Word应用程序变量   
            Word._Application wordApp;
            //Word文档变量
            Document newWordDoc;

            object readOnly = false;
            object isVisible = false;

            //初始化
            //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
            wordApp = new Application();

            Object Nothing = System.Reflection.Missing.Value;

            newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            Word._Document openWord;
            openWord = wordApp.Documents.Open(ref sorceDocPath, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            openWord.Select();
            openWord.Sections[1].Range.Copy();

            object start = 0;
            Range newRang = newWordDoc.Range(ref start, ref start);

            //插入换行符   
            //newWordDoc.Sections[1].Range.InsertBreak(ref type);
            newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);
            openWord.Close(ref Nothing, ref Nothing, ref Nothing);
            return newWordDoc;
        }
        /// <summary>
        /// 替换指定Document的内容,并保存到指定的路径
        /// </summary>
        /// <param name="docObject">Document</param>
        /// <param name="savePath">保存到指定的路径</param>
        protected void ReplaceWordDocAndSave(Document docObject, object savePath, List<string> findText, List<string> replaceText)
        {
            object format = WdSaveFormat.wdFormatDocument;
            object readOnly = false;
            object isVisible = false;

            //string strOldText = "{WORD}";
            //string strNewText = "替换后的文本";

            List<string> IListOldStr = findText;
            List<string> IListNewStr = replaceText;

            string[] newStr = IListNewStr.ToArray();
            int i = 0;

            Object Nothing = System.Reflection.Missing.Value;

            Word._Application wordApp = new Application();
            Word._Document oDoc = docObject;

            object FindText, ReplaceWith, Replace;
            object MissingValue = Type.Missing;

            foreach (string str in IListOldStr)
            {
                oDoc.Content.Find.Text = str;
                //要查找的文本
                FindText = str;
                //替换文本
                //ReplaceWith = strNewText;
                ReplaceWith = newStr[i];
                i++;

                //wdReplaceAll - 替换找到的所有项。
                //wdReplaceNone - 不替换找到的任何项。
                //wdReplaceOne - 替换找到的第一项。
                Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;

                //移除Find的搜索文本和段落格式设置
                oDoc.Content.Find.ClearFormatting();
                //执行替换
                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))
               
            }

            oDoc.SaveAs(ref savePath, 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, ref Nothing);
            //关闭wordDoc文档对象    
            oDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            //关闭wordApp组件对象    
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        }

然后调用

       string strServerPath = "e:/test/Model.docx";  //模板路径
            string strSavePath = "e:/test/test2.docx";  //另存为的路径


            List<string> findText = new List<string>();
            List<string> replaceText = new List<string>();

            findText.Add("{title1}");
            findText.Add("{title2}");
            findText.Add("{time}");
            findText.Add("{city}");
            findText.Add("{p1}");
            findText.Add("{p2}");
            replaceText.Add("民用核安全设备无损检验人员资格鉴定考试");
            replaceText.Add("专业泄漏检验技术Ⅱ级试题及答案");
            replaceText.Add("2013-5");
            replaceText.Add("上海");
            replaceText.Add("张三");
            replaceText.Add("李四");
            ReplaceWordDocAndSave(copyWordDoc(strServerPath), strSavePath, findText, replaceText);

基本操作

需要配置com组件,具体配置http://www.cnblogs.com/mengxingxinqing/archive/2013/06/07/3123344.html


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Asp.Net 角色与权限管理-----配置Web.Config发布时间:2022-07-10
下一篇:
ASP.NET:Response.Redirect重定向发布时间: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