主 题: |
asp.net 生成静态页 | |
作 者: | liubaohuazy (手套) |
一下这段代码是从别的地方找来的,原理就是替换tamplate.html文件中用特殊字符标记出的部分
----------------------------tamplate.html------------------------
<html>
<head>
<title>$htmlkey[0]</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body >
<table $htmlkey[1] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">
<tr>
<td width="100%" valign="middle" align="left">
<span style="color: $htmlkey[2];font-size: $htmlkey[3]"><marquee>$htmlkey[4]</marquee></span>
</td>
</tr>
</table>
</body>
</html>
-------------------------
<%@ Page language="c#" Codebehind="CreatHtml.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.CreatHtml" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CreatHtml</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="btnCreate" style="Z-INDEX: 101; LEFT: 576px; POSITION: absolute; TOP: 48px"
runat="server" Text="创建HTML文件"></asp:Button>
<asp:TextBox id="txtContent" style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 80px"
runat="server" TextMode="MultiLine" Height="402px" Width="352px"></asp:TextBox>
<asp:HyperLink id="hyCreateFile" style="Z-INDEX: 103; LEFT: 584px; POSITION: absolute; TOP: 96px"
runat="server" Target="_blank">创建的HTML文件</asp:HyperLink>
<asp:TextBox id="txtTitle" style="Z-INDEX: 104; LEFT: 208px; POSITION: absolute; TOP: 48px" runat="server"
Width="352px"></asp:TextBox>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 48px" runat="server">页面标题</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 128px; POSITION: absolute; TOP: 80px" runat="server">页面内容</asp:Label>
</form>
</body>
</HTML>
----------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
namespace CommonFunction
{
/// <summary>
/// CreatHtml 的摘要说明。
/// </summary>
public class CreatHtml : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtContent;
protected System.Web.UI.WebControls.HyperLink hyCreateFile;
protected System.Web.UI.WebControls.TextBox txtTitle;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button btnCreate;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCreate_Click(object sender, System.EventArgs e)
{
string[] newContent = new string[5];//定义和html标记数目一致的数组
StringBuilder strhtml = new StringBuilder();
try
{
//创建StreamReader对象
using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "\\template.html"))
{
String oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
}
catch(Exception err)
{
//输出异常信息
Response.Write(err.ToString());
}
//为标记数组赋值
newContent[0] = txtTitle.Text;//标题
newContent[1] = "BackColor='#cccfff'";//背景色
newContent[2] = "#ff0000";//字体颜色
newContent[3] = "100px";//字体大小
newContent[4] = txtContent.Text;//主要内容
//根据上面新的内容生成html文件
try
{
//指定要生成的HTML文件
string fname = Server.MapPath("createHTML") +"\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
//替换html模版文件里的标记为新的内容
for(int i=0;i < 5;i++)
{
strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
}
//创建文件信息对象
FileInfo finfo = new FileInfo(fname);
//以打开或者写入的形式创建文件流
using(FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
//把新的内容写到创建的HTML页面中
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
//设置超级链接的属性
hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
}
catch(Exception err)
{
Response.Write (err.ToString());
}
}
}
}