• 一个自动生成html的类


    可以自动读取模板文件后,生成   当前 年/月/日/yyMMddhhmmss.html

    代码如下

    C#

     1private bool  CreateHtml(string strText,string strContent,string strAuthor)
     2  {
     3   string yearString=DateTime.Now.Year.ToString();
     4   string monthString=DateTime.Now.Month.ToString();
     5   string dayString=DateTime.Now.Day.ToString();
     6
     7   string fileName=yearString+monthString+dayString+DateTime.Now.Millisecond.ToString()+".html";
     8   Directory.CreateDirectory(Server.MapPath(""+yearString+"//"+monthString+"//"+dayString+""));
     9  
    10   Encoding code=Encoding.GetEncoding("gb2312");
    11   string temp=Server.MapPath("text.html");
    12   StreamReader sr=null;
    13   StreamWriter sw=null;
    14   string str="";
    15   //读取模板
    16   try
    17   {
    18    sr=new StreamReader(temp,code);
    19    str=sr.ReadToEnd();
    20   }

    21   catch(Exception exp)
    22   {
    23    Response.Write(exp.Message);
    24    Response.End();
    25    sr.Close();
    26   }

    27   //替换
    28   str=str.Replace("ShowArticle",strText);
    29   str=str.Replace("biaoti",strText);
    30   str=str.Replace("content",strContent);
    31   str=str.Replace("author",strAuthor);
    32   
    33   try
    34   {
    35
    36    sw=new StreamWriter(Server.MapPath(""+yearString+"//"+monthString+"//"+dayString+"//"+fileName+""),false,code);
    37    sw.Write(str);
    38    sw.Flush();
    39   }

    40   catch(Exception exp)
    41   {
    42    HttpContext.Current.Response.Write(exp.Message);
    43    HttpContext.Current.Response.End();
    44   }

    45   finally
    46   {
    47    sw.Close();
    48   }

    49   return true;
    50
    51  }

    52
     html页代码
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
     
    <head>
      
    <title>ShowArticle</title>

     
    </head>
     
    <body>
      biaoti
      
    <br>
      content
    <br>
      author
     
    </body>
    </html>
  • 相关阅读:
    反射
    left join 多个表关联时,将表值置换
    distinct 与 group by 去重
    常见错误
    字符串的处理
    排版样式
    VS低版本打开高版本解决方案(如08打开10、12、13版本vs编译的项目)
    Dw CS 破解
    VS2013如何避开安装时IE10的限制
    UVa540 Team Queue
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/819128.html
Copyright © 2020-2023  润新知