ASP.NET动态生成HTML页面
1/*首先建一个html模板,在相应的位置加上标致,如:$Title$,$Content$之类的。
2
3在这里,HTML里的标致为:$htmlFromat[0],$htmlFromat[1]
4
5template.htm //为模板文件*/
6
7void htmlWrite()
8 {
9 string[] format = new string[3]; //定义和html页面数目一直的数组
10 StringBuilder sb = new StringBuilder();
11
12 try
13 {
14 using(StreamReader sr = new StreamReader(Server.MapPath("template.htm")))
15 {
16 string line;
17 while((line = sr.ReadLine())!=null)
18 {
19 sb.Append(line);
20 }
21 sr.Close();
22 }
23 }
24 catch
25 {}
26 format[0]="内容XXXXXX";
27 format[1]="内容YYYYYYY";
28 format[2]="内容ZZZZZZ";
29
30 for(int i=0;i<3;i++)
31 {
32 sb.Replace("$htmlFromat["+i+"]",format[i]);
33 }
34
35 try
36 {
37 string file = DateTime.Now.ToString("yyyyMMddHHssmm")+".html";
38 File.Create(Server.MapPath(file));
39 using(StreamWriter sw = new StreamWriter(Server.MapPath(file),false,Encoding.GetEncoding("GB2312")))
40 {
41 sw.WriteLine(sb);
42 sw.Flush();
43 sw.Close();
44 Response.Write("OK");
45 }
46
47 }
48 catch(Exception er)
49 {
50 er.Message.ToString();
51 }
52 }
53
54
2
3在这里,HTML里的标致为:$htmlFromat[0],$htmlFromat[1]
4
5template.htm //为模板文件*/
6
7void htmlWrite()
8 {
9 string[] format = new string[3]; //定义和html页面数目一直的数组
10 StringBuilder sb = new StringBuilder();
11
12 try
13 {
14 using(StreamReader sr = new StreamReader(Server.MapPath("template.htm")))
15 {
16 string line;
17 while((line = sr.ReadLine())!=null)
18 {
19 sb.Append(line);
20 }
21 sr.Close();
22 }
23 }
24 catch
25 {}
26 format[0]="内容XXXXXX";
27 format[1]="内容YYYYYYY";
28 format[2]="内容ZZZZZZ";
29
30 for(int i=0;i<3;i++)
31 {
32 sb.Replace("$htmlFromat["+i+"]",format[i]);
33 }
34
35 try
36 {
37 string file = DateTime.Now.ToString("yyyyMMddHHssmm")+".html";
38 File.Create(Server.MapPath(file));
39 using(StreamWriter sw = new StreamWriter(Server.MapPath(file),false,Encoding.GetEncoding("GB2312")))
40 {
41 sw.WriteLine(sb);
42 sw.Flush();
43 sw.Close();
44 Response.Write("OK");
45 }
46
47 }
48 catch(Exception er)
49 {
50 er.Message.ToString();
51 }
52 }
53
54