asp.net替换静态模版生成html页
2010-04-25 11:17
411 查看
在工程中放一个template文件夹,里面有一个静态模版和图片(当然位置什么的,你自己随便,我做的例子是这样的。。。)
![](http://hi.csdn.net/attachment/201004/25/0_12721661716h9Z.gif)
template1.html模版的html代码:
html里面有$htmlformat[0] 这是要替换的标签
里面有一个Default.aspx假设点击一个button触发替换静态模版生成html页,代码如下:
![](file:///C:/DOCUME%7E1/ADMINI%7E1/LOCALS%7E1/Temp/moz-screenshot-1.png)
![](http://hi.csdn.net/attachment/201004/25/0_12721661716h9Z.gif)
template1.html模版的html代码:
<html> <head> <title >$htmlformat[0]</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <table style=" background:url($htmlformat[1])" mce_style=" background:url($htmlformat[1])" height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000" mce_style="border:1px solid #000000"> <tr> <td width="100%" valign="middle" align="left"> <span style="color:$htmlformat[2];font-size: $htmlformat[3]" mce_style="color:$htmlformat[2];font-size: $htmlformat[3]">$htmlformat[4]</span> </td> </tr> </table> </body> </html>
html里面有$htmlformat[0] 这是要替换的标签
里面有一个Default.aspx假设点击一个button触发替换静态模版生成html页,代码如下:
//----将html模版读入StringBuilder对象里面 StringBuilder htmltext = new StringBuilder(); try { using (StreamReader sr = new StreamReader(Server.MapPath("~/template/template1.html"))) { string line; while((line=sr.ReadLine())!=null) { htmltext.Append(line); } sr.Close(); } } catch { Response.Write("<mce:script type="text/javascript"><!-- alert('读取模版错误') // --></mce:script>"); } ////---------------------给标记数组赋值------------ string[] format = new string[5];//定义和模板中标记数目一致的数组 format[0] = "模版"; format[1] = Server.MapPath("~/template/002.jpg");//背景图片 format[2] = "#990099";//字体颜色 format[3] = "100px";//字体大小 format[4] = "<marquee>" + "原来这样真的可以啊,哈哈。。。" + "</marquee>";//文字说明 ////----------替换htm里的标记为你想加的内容 for (int i = 0; i < format.Length;i++ ) { htmltext.Replace("$htmlformat["+i+"]", format[i]); } ////----------生成htm文件------------------―― try { using (StreamWriter sw = new StreamWriter(Server.MapPath("~/template/a.html"), false, System.Text.Encoding.GetEncoding("GB2312"))) { sw.WriteLine(htmltext); sw.Flush(); sw.Close(); } } catch { Response.Write("<mce:script type="text/javascript"><!-- alert('写入模版错误') // --></mce:script>"); }
![](file:///C:/DOCUME%7E1/ADMINI%7E1/LOCALS%7E1/Temp/moz-screenshot-1.png)
![](file:///C:/DOCUME%7E1/ADMINI%7E1/LOCALS%7E1/Temp/moz-screenshot.png)
相关文章推荐
- ASP.Net生成静态HTML页
- [收藏]ASP.Net生成静态HTML页 选择自 coofucoo 的 Blog
- ASP.NET生成静态页面,标签替换方法
- [收藏]ASP.Net生成静态HTML页
- ASP.Net生成静态HTML页,
- ASP.Net生成静态HTML页!
- ASP.Net生成静态HTML页
- 【C#】ASP.Net生成静态HTML页
- ASP.Net生成静态HTML页
- ASP.Net生成静态HTML页!
- ASP.Net生成静态HTML页!
- [收藏]ASP.Net生成静态HTML页
- ASP.Net生成静态HTML页 2
- ASP.Net生成静态HTML页!
- ASP.Net生成静态HTML页
- ASP.Net生成静态HTML页!
- asp.net生成静态html页
- ASP.NET 生成静态html页
- ASP.Net生成静态HTML页
- ASP.NET技巧:利用模版生成html页