跟小D每日学口语

一个自动生成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>
posted @ 2007-07-15 23:10  Danny Chen  阅读(822)  评论(1编辑  收藏  举报