我们直接看代码
首先是在网站根目录中建立一个html文件夹,然后建立一个Temp.html文件代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">
<html xmlns="
http://www.w3.org/1999/xhtml
">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>生成的静态页面</title>
<style type="text/css">
body {
background-color: #FFFFFF;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
font-size:12px;
.tabtext {color: #FFFFFF}
</style>
</head>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td width="300">logo</td>
<td align="right" valign="bottom">
<table width="600" border="0" cellpadding="4" cellspacing="0">
<tr align="center">
<td width="120" bgcolor="#4279BD" class="tabtext">使用帮助</td>
<td width="1"></td>
<td width="120" bgcolor="#EFEFEF"><a href="
mailto:jusmery@163.com
" target="_blank">
联系我们</a></td>
<td width="1"></td>
</table>
<tr bgcolor="#4279BD">
<td height="5" colspan="2"></td>
</table>
<table align="center" width="75%">
<td style="text-align:center; background-color:#4279BD; color :White; font-size:26px; line-height:65px;">
$title$</td>
<td style="color:skyBlue;"> $content$</td>
</table>
</body>
</html>
然后建立Autohtml.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Autohtml.aspx.cs" Inherits="yamblove_admin_Autohtml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">
<html xmlns="
http://www.w3.org/1999/xhtml
">
<head runat="server">
<title>自动生成html静态页面</title>
</head>
<form id="form1" runat="server">
标题:<asp:TextBox ID="txtTitle" runat="server" Width="352px"></asp:TextBox><br />
内容:<asp:TextBox ID="txtContent" runat="server" Height="179px" TextMode="MultiLine"
Width="350px"></asp:TextBox><br />
<asp:Button ID="SenddataBtn" runat="server" OnClick="sendBtn_Click"
Text="提交数据模板自动生成html页面" /><br />
Url地址:<asp:TextBox ID="txtUrl" runat="server" ToolTip="请确认Url地址的存在" Width="359px"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="根据Url地址生成html页面"
OnClick="Button2_Click" />
</form>
</body>
</html>
其Autohtml.aspx.cs 页面代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Text;
using System.IO;
public partial class yamblove_admin_Autohtml : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void sendBtn_Click(object sender, EventArgs e)
//源码是替换掉模板中的特征字符
string mbPath = Server.MapPath("Temp.html");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
catch (Exception ex)
throw ex;
finally
sr.Close();
//根据时间自动重命名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
str = str.Replace("$title$", txtTitle.Text);//替换Title
str = str.Replace("$content$", txtContent.Text);//替换content
//生成静态文件
sw = new StreamWriter(Server.MapPath("html/") + fileName, false, code);
sw.Write(str);
sw.Flush();
catch (Exception ex)
throw ex;
finally
sw.Close();
Response.Write("恭喜<a href=html/" + fileName + " target=_blank>" + fileName + "</a>已经生成成功,保存在html文件夹下!");
//根据Url地址生成静态页保持
protected void Button2_Click(object sender, EventArgs e)
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//读取远程路径
WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
sr = new StreamReader(myTemp.GetResponseStream(), code);
str = sr.ReadToEnd();
catch (Exception ex)
throw ex;
finally
sr.Close();
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
sw = new StreamWriter(Server.MapPath("html/") + fileName, false, code);
sw.Write(str);
sw.Flush();
catch (Exception ex)
throw ex;
finally
sw.Close();
Response.Write("恭喜<a href=html/" + fileName + " target=_blank>" + fileName + "</a>已经生成成功,文件保存在html文件夹下!");