杂七杂八!

C# .Net Sql AD Flash Javascript Ajax
根据html页面模板动态生成html页面(c#类)(转)

 

1:建立一个html页面模板,在这个页面中把你想要动态显示的地方用特殊的字符串表示(如$htmlstrstr$);
2:在程序中用将这个html页面读到一个字符串变量如str;
3:用字符串的replace方法将在第一步中特殊字符替换成你想要的内容;
4保存;

代码
using System;
using System.Text;
using System.Web;
using System.Configuration;
using System.IO;
namespace solucky
{
      /// <summary>
      /// AspxToHtml 的摘要说明。
     /// 注:使用此类,你可以在web.config文件对模板类进行配置.如下
     /*<appSettings>
   <add key="templateFilePath" value="htmlmoudel.htm" />
   <add key="htmlFilePath" value="new/"></add>
   <add key="ErrLogPath" value="aspxTohtml_log.txt"></add>   
   </appSettings>*/
     /// </summary>
     public class AspxToHtml
     {
         /// <summary>
         /// 模板文件中要替代的参数个数
         /// </summary>
         private int            _templateParamCount=0;
         /// <summary>
         /// 模板文件所在的路径
         /// </summary>
         private string        _templateFilePath        =ConfigurationSettings.AppSettings["templateFilePath"];
         /// <summary>
         /// 转换后的html文件所存放的路径
         /// </summary>
         private string        _htmlFilePath            =ConfigurationSettings.AppSettings["htmlFilePath"];
        
         /// <summary>
         /// 模板页页面编码
         /// </summary>
         private Encoding _templateHtmlCode            =Encoding.GetEncoding("gb2312");

         /// <summary>
         /// 转换后的文件编码
         /// </summary>
         private Encoding _code = Encoding.GetEncoding("gb2312");

         /// <summary>
         /// 转换后的html文件名
         /// </summary>
         private string _convertedFilename="";
         /// <summary>
         /// 模板文件中的参数
         /// </summary>
         private string[]    _templateFileparameter    ;
        
         /// <summary>
         /// aspx文件中的要代替HTML文件中的参数实际值
         /// </summary>
         private string[]    _aspxFileparameter;

         private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"];

         属性#region 属性
        
         /// <summary>
         /// 模板文件中要替代的参数个数
         /// </summary>
         public int TemplateParamCount
         {
             get
             {
                 return    this._templateParamCount;
             }
             set//分配参数个数时,同时为模板文件中的参数和aspx文件中的要代替HTML文件中的参数实际值这两个分配实际数组
             {
                 if (value < 0)
                     throw new ArgumentException();

                 if(value>0)               
                 {
                     this._templateParamCount=value;
                     //模板文件中的参数                   
                     _templateFileparameter    = new string[value];
                     //aspx文件中的要代替HTML文件中的参数实际值
                     _aspxFileparameter        = new string[value];
                 }
                 else
                     this._templateParamCount=0;
             }
         }
        
         /// <summary>
         /// 模板文件所在的路径
         ///
         /// </summary>
         public string TemplateFilePath
         {
             get{    return this._templateFilePath;}
             set{    this._templateFilePath=value;}
         }
         /// <summary>
         /// 转换后的html文件所存放的路径
         /// </summary>
         public string HtmlFilePath
        {
            get{    return this._htmlFilePath;}
            set{    this._htmlFilePath=value;}
        }
        /// <summary>
        /// html模板文件编码
        /// </summary>
        public Encoding TemplateHtmlCode
        {
            get{    return this._templateHtmlCode;}
            set{    this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}
        }
        /// <summary>
        /// 编码
        /// </summary>
        public Encoding Code
        {
            get{    return this._code;}
            set{    this._code=Encoding.GetEncoding(value.ToString());}
        }
        /// <summary>
        /// 错误文件所在路径
        /// </summary>
        public string ErrLogPath
        {
            get{
                if(!(this._errlogPath==null))
                    return this._errlogPath;
                else
                    return "aspxTohtml_log.txt";
            }
            set{this._errlogPath=value;}
        }

       
        #endregion
       
        #region 操作

        /// <summary>
        /// 获取转换后的html文件所在相对文件路径
        /// 如:如果HtmlFilePath="/news/"
        /// 转换后的html文件名为200505050505.html
        /// 则返回的值为/news/200505050505.html
        /// </summary>
        /// <remarks>如果在未调用StartConvert方法之前调用此属性则返回null</remarks>
        public string HtmlFileVirtualPath
        {
            get
            {   
                if(!(this._convertedFilename==""))
                    return    this.HtmlFilePath+this._convertedFilename;
                else
                    return null;
            }
        }

        /// <summary>
        /// 为HTML页面参数数组付值
        /// </summary>
        /// <param name="param"></param>
        public void    setTemplateFileparameter(string[] param)
        {
            try
            {
                if(param.Length==this.TemplateParamCount)
                    this._templateFileparameter=param;
                //else//与原定义的个数不等
                    //
            }
            catch(System.Exception    ex)
            {
                WriteErrFile(ex);
            }
        }
        /// <summary>
        /// 为aspx文件中将要替换html文件中的参数数组付值
        /// </summary>
        /// <param name="param"></param>
        public void setAspxFileparameter(string[] param)
        {
            try
            {
                if(param.Length==this.TemplateParamCount)
                    this._aspxFileparameter=param;
                //else//与原定义的个数不等
                //
            }
            catch(System.Exception    ex)
            {
            WriteErrFile(ex);
            }
        }
        /// <summary>
        /// 开始进行aspxTohtml转换
        /// </summary>
        /// <returns>返回值为成功创建后的文件名称</returns>
        /// <remarks>在调用此方法之前必需确定已调用setTemplateFileparameter 和setAspxFileparameter方法进行相应的付值操作</remarks>
        public string StartConvert()
        {
            if(this._templateFileparameter.Length==this._aspxFileparameter.Length)
            {
                return writeFile();
            }
            else{
                return null;
            }
        }
        /// <summary>
        /// 开始进行aspxTohtml转换
        /// </summary>
        /// <param name="htmlparam">html模板页中的所有参数数组</param>
        /// <param name="aspxparam">aspx页面中要代替html模板页中参数值数组</param>
        /// <returns>返回值为成功创建后的文件名称</returns>
        public string StartConvert(string[] htmlparam,string[] aspxparam)
        {
            //先调用setTemplateFileparameter 和setAspxFileparameter方法,进行付值操作
            setTemplateFileparameter(htmlparam);
            setAspxFileparameter(aspxparam);
            //
            string fn=this.StartConvert();
            //
            _convertedFilename=fn;
            //
            return fn;
        }
       
        /// <summary>
        /// 用时间加随机数生成一个文件名
        /// </summary>
        /// <returns></returns>
        private string getfilename()
        {
            //用时间加随机数生成一个文件名
            System.Threading.Thread.Sleep(50);
            string yearStr = System.DateTime.Now.Year.ToString();
            string monthStr = string.Format("{0:0#}",System.DateTime.Now.Month);
            string dayStr = string.Format("{0:0#}",System.DateTime.Now.Day);
            string hourStr = string.Format("{0:0#}",System.DateTime.Now.Hour);
            string minuteStr = string.Format("{0:0#}",System.DateTime.Now.Minute);
            string secondStr = string.Format("{0:0#}",System.DateTime.Now.Second);
            string millisecondStr = string.Format("{0:000#}",System.DateTime.Now.Millisecond);                   
            System.Random rd = new System.Random();
            return yearStr + monthStr + dayStr + hourStr + minuteStr + secondStr + millisecondStr + string.Format("{0:0000#}",rd.Next(100))+".html";
            //return DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
        }
        /// <summary>
        /// 进行转换处理
        /// </summary>
        /// <returns>返回以时间命名的文件名</returns>
        private string writeFile()
        {
           
            // 读取模板文件
            string temp = HttpContext.Current.Server.MapPath(this.TemplateFilePath);
            StreamReader sr=null;           
            string str="";
            try
            {
                sr = new StreamReader(temp, this.TemplateHtmlCode);
                str = sr.ReadToEnd(); // 读取文件
            }
            catch(Exception ex)
            {
                //HttpContext.Current.Response.Write(exp.Message);
                //HttpContext.Current.Response.End();       
                WriteErrFile(ex);
            }
            finally
            {
                sr.Close();
            }           
            // 替换内容
            // 这时,模板文件已经读入到名称为str的变量中了
            for(int i=0;i<this.TemplateParamCount;i++)
            {
                str =str.Replace(this._templateFileparameter[i],this._aspxFileparameter[i]);
            }       

            return savefile(str);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>

        private string savefile(string str)
        {
            // 写文件
            StreamWriter sw=null;
            try
            {
               
                string path = HttpContext.Current.Server.MapPath(this.HtmlFilePath);
                //html文件名称   
                string htmlfilename=getfilename();
                sw = new StreamWriter(path + htmlfilename , false, this.Code);
                sw.Write(str);
                sw.Flush();
                return htmlfilename;
            }
            catch(Exception ex)
            {               
                WriteErrFile(ex);
            }
            finally
            {
                sw.Close();
            }
            return "";
        }

        /// <summary>
        /// 传入URL返回网页的html代码
        /// </summary>
        /// <param name="Url">URL</param>
        /// <returns></returns>
        public string getUrltoHtml(string Url)
        {           
            try
            {
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);           
                System.Net.WebResponse wResp =wReq.GetResponse();               
                System.IO.Stream respStream = wResp.GetResponseStream();               
                System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
                return savefile(reader.ReadToEnd());

            }
            catch(System.Exception ex)
            {
                WriteErrFile(ex);
            }
            return "";
        }
        #endregion


        #region 构造       
       
        public AspxToHtml()
        {
            //
            // TODO: 在此处添加构造函数逻辑
           //           
     }

        private void settemplateParamCount(int templateParamCount)
        {
          if (templateParamCount>0)
               this.TemplateParamCount=templateParamCount;
        }
        /// <summary>
        /// 提供欲代替的参数个数
        /// </summary>
        /// <param name="templateParamCount"></param>
        public AspxToHtml(int templateParamCount)
        {   
            settemplateParamCount(templateParamCount);
           
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="templateParamCount">html模板页中的参数个数</param>
        /// <param name="htmlFilePath">生成的html文件所存放的文件夹路径</param>
        /// <param name="templateFilePath">html模板页路径</param>
        public AspxToHtml(int templateParamCount,string htmlFilePath,string templateFilePath)
        {
            settemplateParamCount(templateParamCount);
            this.HtmlFilePath        =    htmlFilePath;
            this.TemplateFilePath    =    templateFilePath;
           
        }
        #endregion

        #region 把错误写入文件方法
       
        /// <summary>
        /// 把错误写入文件方法
        /// </summary>
        /// <param name="ee"></param>
        private void WriteErrFile(Exception ee)
        {
           
            FileStream fs1 = new FileStream(HttpContext.Current.Server.MapPath(ErrLogPath), System.IO.FileMode.Append);
            StreamWriter sw1 = new StreamWriter(fs1);
            sw1.WriteLine("**************************************************");
            sw1.WriteLine("错误日期:" + System.DateTime.Now);
            sw1.WriteLine("错误描述:" + ee.Message);
            sw1.WriteLine("错误名称:" + ee.Source);
            sw1.WriteLine("详细:" + ee.ToString());
            sw1.WriteLine("*************************************************");
            sw1.Close();
        }
        #endregion
    }
}

 

posted on 2009-07-18 00:13  acheng  阅读(458)  评论(0编辑  收藏  举报