• 通过xml模板生成意见审批表word文件


    在做发文流程时,用户提出需把一个业务流程中的审批人及其意见保存成一个意见审批表(固定格式的红头文件)

    如下图:

    参考文章如下:

    http://bbs.csdn.net/topics/360026952

    1:制定xml模板,在word里画好表格后把他另存为xml文件,再用word打开该xml文件,在后面要放数据库值的地方输入FileTitel等value(这些字不要和xml文件里已经有的字符串重复了),因为你后面只是把filetitle替换成数据库的值而已.

    2:写代码读该xml在把,xml字符串中写好的那些值替换成你想要的值,再存成doc文件就可以了

    类代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    
    namespace Study.Word
    {
        public class CreateWord
        {
            /// <summary>
            /// 读取Word模板的XML文件,替换标签保存Word
            /// </summary>
            /// <param name="sSourceDocUrl">模板地址</param>
            /// <param name="sSaveDocUrl">保存地址</param>
            /// <param name="list">标签对应文本集合</param>
            /// <param name="sErrMsg">错误信息</param>
            /// <returns></returns>
            public bool DoWordXmlANDSave(string sSourceDocUrl, string sSaveDocUrl, List<MarkWithText> list, ref string sErrMsg)
            {
                return WirteToDOC(ReplaceMarkToText(ReadToString(sSourceDocUrl), list), sSaveDocUrl, ref sErrMsg);
            }
    
            private string ReadToString(string sFileFullUrl)
            {
                StreamReader sReader = new StreamReader(sFileFullUrl);
                return sReader.ReadToEnd();
            }
    
            private string ReadToString(Stream sStream)
            {
                return "";
            }
    
            private string ReplaceMarkToText(string sSourceText, List<MarkWithText> list)
            {
                if (string.IsNullOrEmpty(sSourceText))
                    return "";
                if (list.Count == 0)
                    return sSourceText;
    
                string sReturnText = sSourceText;
    
                foreach (MarkWithText mt in list)
                {
                    sReturnText = sReturnText.Replace(mt.BookMark, mt.ReplaceValue);
                }
    
                return sReturnText;
            }
    
            private bool WirteToDOC(string sSourceText, string sFileFullUrl, ref string sErrMsg)
            {
    
                if (!sFileFullUrl.ToUpper().EndsWith(".DOC"))
                    sFileFullUrl = sFileFullUrl + ".doc";
                try
                {
                    File.WriteAllText(sFileFullUrl, sSourceText);
                    return true;
                }
                catch (Exception ex)
                {
    
                    sErrMsg += "|Function:WirteToDOC ErrMeg:" + ex.Message;
                    return false;
                }
    
            }
    
        }
    
    
        /// <summary>
        /// 书签对应文本
        /// </summary>
        public class MarkWithText
        {
            public MarkWithText(string BookMark, string ReplaceValue)
            {
                this.BookMark = BookMark;
                this.ReplaceValue = ReplaceValue;
            }
    
            string _BookMark;//书签名称
            string _ReplaceValue;//替换的文本
    
            /// <summary>
            /// 书签名称
            /// </summary>
            public string BookMark
            {
                get { return _BookMark; }
                set { _BookMark = value; }
            }
    
            /// <summary>
            /// 替换的文本
            /// </summary>
            public string ReplaceValue
            {
                get { return _ReplaceValue; }
                set { _ReplaceValue = value; }
            }
    
        }
    }
    

     前台按钮代码:

    protected void Button1_Click(object sender, EventArgs e)
            {
                CreateWord createWord = new CreateWord();
                List<MarkWithText> list = new List<MarkWithText>();
                string strFromPath = Server.MapPath("/Word/Test.xml");
                string strToPath = Server.MapPath("/Word/NewTest.doc");
                
                list.Add(new MarkWithText("FileDeptOther", "xxzx"));
                list.Add(new MarkWithText("Snow", "Suzm"));
                list.Add(new MarkWithText("Sportdog", "Suzm"));
    
    
    
                string strMsg = "";
                createWord.DoWordXmlANDSave(strFromPath, strToPath, list, ref strMsg);
            }
    

    很简单是吧.

  • 相关阅读:
    win10没有新建文件夹
    js暂停的函数
    Mini ORM——PetaPoco笔记
    阻止网页内部滚动条mousewheel事件冒泡
    setTimeout调用带参数的函数的方法
    spring入门--Spring框架底层原理
    spring入门——applicationContext与BeanFactory的区别
    mybatis的Sql语句打印
    Sql语句中IN和exists的区别及应用
    Jenkins学习
  • 原文地址:https://www.cnblogs.com/sportdog/p/2805128.html
Copyright © 2020-2023  润新知