工作小结(八)-生成静态HTML
生成静态的HTML页面,感觉蛮有用的。
1.静态模板:
2.后台代码:Code
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<title>$htmlkey[0]</title>
5<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
6<meta name=ProgId content=VisualStudio.HTML>
7<meta name=Originator content="Microsoft Visual Studio .NET 7.1">
8</head>
9<body>
10<table height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor=#3366ff>
11<tr>
12<td>
13<span style="color:$htmlkey[2];font-size:$htmlkey[3]"><marquee>$htmlkey[4]</marquee></span>
14</td>
15</tr>
16</table>
17</body>
18</html>
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<title>$htmlkey[0]</title>
5<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
6<meta name=ProgId content=VisualStudio.HTML>
7<meta name=Originator content="Microsoft Visual Studio .NET 7.1">
8</head>
9<body>
10<table height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor=#3366ff>
11<tr>
12<td>
13<span style="color:$htmlkey[2];font-size:$htmlkey[3]"><marquee>$htmlkey[4]</marquee></span>
14</td>
15</tr>
16</table>
17</body>
18</html>
Code
1 string[] newContent=new string[5];//定义和HTML标记数目一致得数组
2 StringBuilder strhtml=new StringBuilder();
3 try
4 {
5 //创建StreamReader对象
6 using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "\\template.html"))
7 {
8 String oneline;
9 //读取指定的HTML文件模板
10 while((oneline=sr.ReadLine())!=null)
11 {
12 strhtml.Append(oneline);
13 }
14 //sr.Close;
15 }
16 }
17 catch(Exception err)
18 {
19 //输出异常
20 Response.Write(err.ToString());
21 }
22
23 //为标记数组赋值
24 newContent[0]=this.TextBox1.Text;//标题
25 newContent[1]="BackColor='#cccfff'";//背景色
26 newContent[2]="#ff0000";//字体颜色
27 newContent[3]="100px";//字体大小
28 newContent[4]=this.TextBox2.Text;//主要内容
29 //根据上面新得内容生成html文件
30
31 try
32 {
33 //指定要生成的html文件
34 string fname=Server.MapPath("createHTML")+"\\"+"a.html";//这里后缀可以随便改 //替换html模板文件中里的标记为新的内容
35 for(int i=0;i<5;i++)
36 {
37 strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
38 }
39 //创建文件信息对象
40 FileInfo finfo=new FileInfo(fname);
41 //以打开或者写入的形式创建文件流
42 using(FileStream fs=finfo.OpenWrite())
43 {
44 //根据上面创建的文件流创建写数据流
45 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
46 //把新的内容写到创建的html页面中
47 sw.WriteLine(strhtml);
48 sw.Flush();
49 sw.Close();
50 }
51
52 }
53 catch(Exception err)
54 {
55 Response.Write(err.ToString());
56 }
1 string[] newContent=new string[5];//定义和HTML标记数目一致得数组
2 StringBuilder strhtml=new StringBuilder();
3 try
4 {
5 //创建StreamReader对象
6 using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "\\template.html"))
7 {
8 String oneline;
9 //读取指定的HTML文件模板
10 while((oneline=sr.ReadLine())!=null)
11 {
12 strhtml.Append(oneline);
13 }
14 //sr.Close;
15 }
16 }
17 catch(Exception err)
18 {
19 //输出异常
20 Response.Write(err.ToString());
21 }
22
23 //为标记数组赋值
24 newContent[0]=this.TextBox1.Text;//标题
25 newContent[1]="BackColor='#cccfff'";//背景色
26 newContent[2]="#ff0000";//字体颜色
27 newContent[3]="100px";//字体大小
28 newContent[4]=this.TextBox2.Text;//主要内容
29 //根据上面新得内容生成html文件
30
31 try
32 {
33 //指定要生成的html文件
34 string fname=Server.MapPath("createHTML")+"\\"+"a.html";//这里后缀可以随便改 //替换html模板文件中里的标记为新的内容
35 for(int i=0;i<5;i++)
36 {
37 strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
38 }
39 //创建文件信息对象
40 FileInfo finfo=new FileInfo(fname);
41 //以打开或者写入的形式创建文件流
42 using(FileStream fs=finfo.OpenWrite())
43 {
44 //根据上面创建的文件流创建写数据流
45 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
46 //把新的内容写到创建的html页面中
47 sw.WriteLine(strhtml);
48 sw.Flush();
49 sw.Close();
50 }
51
52 }
53 catch(Exception err)
54 {
55 Response.Write(err.ToString());
56 }
经验小结:
1.注意模板编码,将中文编码全部替换成英文的,且模板不能破坏,一旦破坏,就必须删除重新创建。
2.若在生成静态HTML页面的下面生成了许多无用的东西,把静态HTML删掉,再生成就OK了。注:可以在程序中作处理,如先判断是否存在,存在则删除。
3.可以根据需要,把中间相同的HTML在模板中用一个标记,然后在后台写HTML来替换。