zoukankan      html  css  js  c++  java
  • C#操作word模板

      

     string newDocFileName = Guid.NewGuid().ToString().Replace("-", "");
            string strServerPath = Server.MapPath("") + "\\Model.doc";  //模板路径
            string strSavePath = Server.MapPath("Doc") + "\\" + newDocFileName + ".doc";  //另存为的路径
            strURL = strServerPath;

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

            findText.Add("{NAME}");
            findText.Add("{AGE}");
            findText.Add("{SEX}");

            replaceText.Add("姓名");
            replaceText.Add("24岁");
            replaceText.Add("男");
            newDocFileName = "Doc/7c37c538b92749eeaf55efab579f2c3e.doc";
            ReplaceWordDocAndSave(copyWordDoc(strServerPath), strSavePath, findText, replaceText);

        /// <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应用程序变量   
            Application wordApp;
            //Word文档变量
            Document newWordDoc;

            object readOnly = false;
            object isVisible = false;

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

            Object Nothing = System.Reflection.Missing.Value;

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

            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;

            Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass();
            Microsoft.Office.Interop.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.wdReplaceAll;

                //移除Find的搜索文本和段落格式设置
                oDoc.Content.Find.ClearFormatting();

                if (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))
                {
                    Response.Write("替换成功!");
                    Response.Write("<br>");
                }
                else
                {
                    Response.Write("没有相关要替换的:(" + str + ")字符");
                    Response.Write("<br>");
                }
            }

            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);
        }

  • 相关阅读:
    win7 64位下 mongodb安装及命令运行
    CYQ.Data 快速开发EasyUI
    面试杂记:三个月的面试回忆录(携程、腾讯等)
    那点你不知道的XHtml(Xml+Html)语法知识(DTD、XSD)
    .NET各大平台数据列表控件绑定原理及比较(WebForm、Winform、WPF)
    CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
    CYQ.Data V4系列全面开源(2013-08-04)
    C#与Java对比学习:类型判断、类与接口继承、代码规范与编码习惯、常量定义
    C#与Java对比学习:数据类型、集合类、栈与队列、迭达、可变参数、枚举
    突破瓶颈,对比学习:Eclipse开发环境与VS开发环境的调试对比
  • 原文地址:https://www.cnblogs.com/ajun/p/2304121.html
Copyright © 2011-2022 走看看