zoukankan      html  css  js  c++  java
  • 一个自动生成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>
  • 相关阅读:
    QML类型说明-ParallelAnimation
    QML基本可视化元素--Text
    qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)
    JavaScript toFixed() 方法
    FM/AM收音机原理
    Q_INVOKABLE与invokeMethod用法全解
    SQL问题(面试题)
    数组、List和ArrayList的区别
    catch/finally中不应使用 writer.flush()
    Spring AOP整理
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/819128.html
Copyright © 2011-2022 走看看