您的位置:首页 > 编程语言 > ASP

asp.net替换静态模版生成html页

2010-04-25 11:17 411 查看
在工程中放一个template文件夹,里面有一个静态模版和图片(当然位置什么的,你自己随便,我做的例子是这样的。。。)



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>");
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: