如何实现自动生成HTML网页

html-css016

如何实现自动生成HTML网页,第1张

我理解你的问题是生成动态网页是吧。一般动态的HTML是通过支持CGI 即 (通用网关接口)的语言生成的。例如 PHP  ASP  PYTHON  RUBY等。当然前段的JS 也可以通过DOM 生成动态HTML

所谓HTML实际上是一种标记语言。而后端脚本语言通常可以控制 终端设备输出显示。这样利用输出加上通用网关接口就可以实现 动态的HTML输出(在这里实际上用户浏览器就是一个终端。)

一般后台语言生成HTML都是通过模板实现 例如PYTHON

# ! / u s r / b i n / e n v p y t h o n 

import cgi 

 reshtml = '''Content-Type: text/html\n 

<HTML><HEAD><TITLE> 7 Friends CGI Demo (dynamic screen) 

</TITLE></HEAD> 

<BODY><H3>Friends list for: <I>%s</I></H3>   

Your name is: <B>%s</B><P> 

You have <B>%s</B> friends. 

  </BODY></HTML>''' 

 form = cgi.FieldStorage() 

who = form['person'].value  howmany = form['howmany'].value   print reshtml %

(who, who, howmany) 

自动生成?

1,编写html文件的内容

2,将编写的文件内容写入一个文件中

3,保存文件为.html文件到指定目录

4,根据路径调用生成的html文件

using System

using System.Data

using System.Configuration

using System.Web

using System.Web.Security

using System.Web.UI

using System.Web.UI.WebControls

using System.Web.UI.WebControls.WebParts

using System.Web.UI.HtmlControls

using System.IO

using System.Text

/// <summary>

/// WriteFile 的摘要说明

/// </summary>

public class WriteFile

{

public WriteFile()

{

}

public static bool createHtml(string[] strnewsHtml,string[] stroldHtml,string strModeFilePath,string strPath)

{

bool flag = false

StreamReader sr = null

StreamWriter sw = null

string filepath = HttpContext.Current.Server.MapPath(strModeFilePath)

Encoding code = Encoding.GetEncoding("gb2312")

string s = string.Empty

try

{

sr = new StreamReader(filepath,code)

s = sr.ReadToEnd()

}

catch (Exception ex)

{

throw ex

}

finally

{

sr.Close()

}

try

{

for (int i = 0i <strnewsHtml.Lengthi++)

{

s = s.Replace(stroldHtml[i], strnewsHtml[i])

}

sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code)

sw.Write(s)

flag = true

}

catch (Exception ex)

{

flag = false

throw ex

}

finally

{

sw.Flush()

sw.Close()

}

return flag

}

public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)

{

bool Flage = false

StreamReader ReaderFile = null

StreamWriter WrirteFile = null

string FilePath = HttpContext.Current.Server.MapPath(strHtml)

Encoding Code = Encoding.GetEncoding("gb2312")

string strFile = string.Empty

try

{

ReaderFile = new StreamReader(FilePath, Code)

strFile = ReaderFile.ReadToEnd()

}

catch (Exception ex)

{

throw ex

}

finally

{

ReaderFile.Close()

}

try

{

int intLengTh = strNewsHtml.Length

for (int i = 0i <intLengThi++)

{

int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length

int intEnd = strFile.IndexOf(strEndHtml[i])

string strOldHtml = strFile.Substring(intStart, intEnd - intStart)

strFile = strFile.Replace(strOldHtml, strNewsHtml[i])

}

WrirteFile = new StreamWriter(FilePath, false, Code)

WrirteFile.Write(strFile)

Flage = true

}

catch (Exception ex)

{

throw ex

}

finally

{

WrirteFile.Flush()

WrirteFile.Close()

}

return Flage

}

}

调用公共类:

----------------------------------------------------------------------------

protected void Button2_Click(object sender, EventArgs e)

{

string NewsTitle = this.TextBox1.Text

string NewsKindName = this.DropDownList1.SelectedItem.Text

string NewsBody = this.WebEditor1.Text

DateTime PubTime = DateTime.Now

string UserName = Session["UserName"].ToString()

Response.Write(NewsKindName)

string[] strNewsHtml = new string[] { NewsTitle, NewsKindName, NewsBody, PubTime.ToString(), UserName }

string[] strOldHtml = new string[] { "@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName" }

string strFileName = DateTime.Now.ToString("ddhhmmss") + ".html"

string strFilePath = string.Format("NewsHtml/{0}", strFileName)

try

{

if (WriteFile.createHtml(strNewsHtml, strOldHtml, "mode.htm", strFilePath))

{

this.Label1.Text = "生成成功!"

}

else

{

this.Label1.Text = "生成失败!"

}

}

catch

{

this.Label1.Text = "生成失败!"

}

}

protected void Button3_Click(object sender, EventArgs e)

{

string[] strNewsHtml=new string[]{"游!"}

string[] strStartHtml=new string[]{"<!-- start -->"}

string[] strEndHtml=new string[]{"<!--end-->"}

if (WriteFile.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html"))

{

this.Label1.Text="生成首页成功!"

}

else

{

this.Label1.Text="生成首页失败!"

}

}

新建文件夹NewsHtml,生成html文件放在里面

-----------------------------------------------------------

增加一个模板文件

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

<title>无标题页</title>

</head>

<body>

<table border="1" cellpadding="0" cellspacing="0" style="width: 522pxheight: 338px">

<tr>

<td align="center" colspan="2">

@Title</td>

</tr>

<tr>

<td align="center" colspan="2">

发布人:@UserName     发布时间:@PubTime      新闻类别:@NewsKInd</td>

</tr>

<tr>

<td colspan="2">

@NewsBody</td></tr><tr>

<td style="WIDTH: 100px">

</td><td style="WIDTH: 100px" >

</td></tr></table></body></html>