使用C#生成静态页面

开发环境:vs2010beta2

使用场景:新闻发布等CMS系统

一、创建用于生成静态页面的html模板文件template.html
放在web应用程序的根目录下

相关代码如下: 

代码
 1 <html>
 2 <head>
 3     <title>$htmlkey[0]</title>
 4     <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 5 </head>
 6 <body >
 7     <table $htmlkey[1] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0"  bgcolor="#eeeeee" style="border:1px solid #000000">
 8     <tr>
 9         <td width="100%" valign="middle" align="left"><span style="color: $htmlkey[2];font-size: $htmlkey[3]">$htmlkey[4]</span>
10         </td>
11     </tr>
12     </table>
13 </body>
14 </html>

二、创建页面文件default.aspx
增加两个Input Text文本框(HTML类型的),分别指定name为Title和Content。
创建标准的按钮


在按钮的click事件中增加代码


 

代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7  using System;
 8 using System.Data;
 9 using System.Configuration;
10 using System.Collections;
11 using System.Web;
12 using System.Web.Security;
13 using System.Web.UI;
14 using System.Web.UI.WebControls;
15 using System.Web.UI.WebControls.WebParts;
16 using System.Web.UI.HtmlControls;
17 using System.IO;
18 using System.Text;
19 
20 public partial class _Default : System.Web.UI.Page
21 {
22     protected void Page_Load(object sender, EventArgs e)
23     {
24     }
25 protected void  Button1_Click1(object sender, EventArgs e)
26 {
27     string strTitle = Request.Form["Title"].ToString();
28     string strContent = Request.Form["Content"].ToString();
29 
30     string dir = Server.MapPath("NewsFiles/" + DateTime.Now.ToString("yyMMdd"));
31     if (!Directory.Exists(dir))
32     {
33         Directory.CreateDirectory(dir);
34     }
35     /**/
36     ///////////////////////////创建当前日期的文件夹结束
37 
38     string[] newContent = new string[5];//定义和html标记数目一致的数组
39     StringBuilder strhtml = new StringBuilder();
40     try
41     {
42         //创建StreamReader对象
43         using (StreamReader sr = new StreamReader(Server.MapPath("NewsFiles/"+ "template.html"))
44         {
45             String oneline;
46             //读取指定的HTML文件模板
47             while ((oneline = sr.ReadLine()) != null)
48             {
49                 strhtml.Append(oneline);
50             }
51             sr.Close();
52         }
53     }
54     catch (Exception err)
55     {
56         //输出异常信息
57         Response.Write(err.ToString());
58     }
59     //为标记数组赋值
60     newContent[0= strTitle;//标题
61     newContent[1= "BackColor='#cccfff'";//背景色
62     newContent[2= "#ff0000";//字体颜色
63     newContent[3= "100px";//字体大小
64     newContent[4= strContent;//主要内容
65 
66     //根据上面新的内容生成html文件
67     try
68     {
69         //指定要生成的HTML文件
70         string fname = Server.MapPath("NewsFiles/" + DateTime.Now.ToString("yyMMdd")) + "/" + DateTime.Now.ToString("yyyymmddhhmmss"+ ".html";
71         //替换html模版文件里的标记为新的内容
72         for (int i = 0; i < 5; i++)
73         {
74             strhtml.Replace("$htmlkey[" + i + "]", newContent[i]);
75         }
76         //创建文件信息对象
77         FileInfo finfo = new FileInfo(fname);
78         //以打开或者写入的形式创建文件流
79         using (FileStream fs = finfo.OpenWrite())
80         {
81             //根据上面创建的文件流创建写数据流
82             StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
83             //把新的内容写到创建的HTML页面中
84             sw.WriteLine(strhtml);
85             sw.Flush();
86             sw.Close();
87         }
88     }
89     catch (Exception err)
90     {
91         Response.Write(err.ToString());
92     }
93 }
94 }
95 

三、运行代码

 

四、查看文件

 

转载于:https://www.cnblogs.com/mingle/archive/2009/11/27/1612182.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值