ndfweb.cn

ASP+FSO+框架實現ASP批量生成HTML並分頁的方法


2009-04-13 15:00:46 (6386)



<!--#include file="conn.asp"-->'數據連接文件
<%
On Error Resume Next'容錯處理
Dim City'定義取得要生成頁麵的城市
City=Request.Querystring("City")'獲取生成的城市酒店值從框架傳過來的在後麵將介紹
HtmlStartName=Request.Querystring("HtmlStartName")'獲得生成htm文件名前綴
Dim sql'搜索字符串,這裏我就直接打開表不用搜索變量了,搜索條件按自己寫就可以
sql="select * from Hotel where [City] = '" & City & "' "
Dim oRs'數據操作對象
Dim PageCounts'實現分頁生成必須得知呀有多少頁
Set oRs = Server.CreateObject("ADODB.Recordset")
oRs.Open Sql,oConn,1,1'找開酒店等於City變量的表
oRs.pagesize=10'十個記錄為一頁
PageCounts=oRs.pagecount'得出要生成多少個頁麵,循環生成使用
Dim fs'定義fso文件對象
Dim folders'存放生成靜態頁的文件夾名稱
Dim Filestart'定義生成htm文件前綴
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Dim i
for i=1 to PageCounts'開始循環生成頁麵,也就是分頁生成了
page=i
oRs.absolutepage=i'頁碼
rowcount=oRs.pagesize'當頁記錄數
folders=server.mappath("CityHtml")
if (fs.FolderExists(folders)) then'判斷文件夾是否存在
else
fs.CreateFolder(folders)'不存在則創建CityHtml文件夾
end if
if i=1 then
Filestart=HtmlStartName'如果為第一頁則定義文件名為傳值名.例如beijing則為beijing.htm
else
Filestart=HtmlStartName&i'如果第二頁則為beijing+1例如有兩頁也就是i等於2則為 beijing2.htm如此類推...(.htm後綴就在後麵加上)
end if
Dim files'定義生成文本文件名稱變量
Dim filez'定義文件路徑名稱變量
files=Filestart&".txt"'本文件名稱
filez=folders&"\"&"files'文本文件路徑
'冊除文件
Dim checkfile'檢查文本文件是否已經存在,是則刪除
checkfile=server.mappath("CityHtml\"&Filestart&".htm")'檢查htm文件是否已經存在,是則刪除
if (fs.FileExists(checkfile)) then'檢查htm文件是否已經存在,是則刪除
Dim df'定義文件對象*刪除文件使用*
Set df=fs.GetFile(checkfile)'定義要冊除的文件
df.delete'冊除文件
end if'判斷結束
Dim ts'定義寫入文件對象
set ts = fs.createtextfile(filez,true) '開啟寫入文件內容**我在正文隻簡單寫入酒店名稱和靜態數字分頁顯示**
ts.write("<Html><Head><Title>生成"&City&"城市酒店</Title>"&vbcrlf)'之後就是要生成的正文件內容了跟使用Response.write
ts.write("<META http-equiv=Content-Type content=text/html; charset=gb2312>"&vbcrlf)
ts.write("<meta name=keywords content="&city&"酒店>"&vbcrlf)
ts.write("<link href='/Style/style.css' rel='stylesheet' type='text/css'></head><body topmargin=0>"&vbcrlf)
ts.Write("<TABLE WIDTH=760 cellspacing=0 cellpadding=0 align=center>"&vbcrlf&_
"<TR><TD width='100%'>"&vbcrlf)
'分頁輸出開始
'數字分頁程序原理在這我就不多說了,不懂的朋友可在網上搜索一下
Dim page'當前頁
Dim Page2'數字分頁變量
Dim s'數字分頁變量
if page=1 then
ts.write (" [首 頁] [前一頁] ")
else
ts.write (" <a href="&HtmlStartName&".htm"&" class=blue>[首 頁]</a> <a href="&HtmlStartName&Replace(page-1,1,"")&".htm"&" class=blue>前一頁</a> ")
end if
page2=(page-(page mod 10))/10
if page2<1 then page2=0
for s=page2*10-1 to page2*10+10
if s>0 then
if s=cint(page) then
ts.write (" <font color='#000000'>["& s & "]</font>")
else
if s=1 then
ts.write (" <a href="&HtmlStartName&replace(s,1,"")&".htm"&" class=blue>["& s &"]</a>")
else
ts.write (" <a href="&HtmlStartName&s&".htm"&" class=blue>["& s &"]</a>")
end if
end if
if s=ors.pagecount then
exit for
end if
end if
next
if cint(page)=ors.pagecount then
ts.write (" [後一頁] [尾 頁]")
else
ts.write (" <a href="&HtmlStartName&page+1&".htm"&" class=blue>[後一頁]</a> <a href="&HtmlStartName&ors.pagecount&".htm"&" class=blue>[尾 頁]</a>")
end if
ts.write("</TD></TR>")
'分頁輸出結束
do while not ors.eof and rowcount>0 '輸出酒店名稱
ts.write("<TR><TD width='100%'>"&oRs.Fields("Chinese_Name")&"</TD></TR>"&vbcrlf)
oRs.movenext
rowcount=rowcount-1'當頁記錄數-1 loop
ts.write("</Table></body></html>"&vbcrlf)
ts.close
set ts=nothing '釋放對象
Dim EditFile'定義改寫文件變量
Set EditFile = fs.GetFile(filez)'設置改寫文件對象
EditFile.name= left(EditFile.name,len(EditFile.name)-4)&".htm" '改寫文本文件成htm
next'循環生成結束(分頁生成)
set EditFile=nothing '釋放對象
set fs=nothing'釋放對象
if err.number<>0 then '處理生成錯誤
Response.write (City&"更新時發生未知錯誤<A href=ToHtml.asp?City="&City&"&HtmlName="&HtmlStartName& ">重新更新</A>")
else
Response.Write(City&"酒店更新已完成 "&Now())
end if
%>



超長文章內容進行分頁並生成HTML文件

以下是程序源代碼:
<%
dim fname,fso,fout,htmlcode
dim htmlbody,htmlbody1,page_n
htmlbody=request.form("body")
page_n=len(htmlbody)-len(replace(htmlbody,"$",""))
’生成文件名的函數
function makefilename(fname)
fname = fname
fname = replace(fname,"-","")
fname = replace(fname," ","")  
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname
end function
if request.querystring("action")="add" then
for i=0 to page_n
fname=makefilename(now())
if i=0 then
ppage=0
else
ppage=i-1
end if
if i=0 and page_n>i then
npage=i+1
else
npage=page_n
end if
shtml=split(htmlbody,"$")(i) & vbcrlf
shtml=shtml&"<br><a href="""&fname&"_"&ppage&".html"">上一頁</a> <a href="""&fname&"_"&npage&".html"">下一頁</a>"
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(fname&"_"&i&".html"))
fout.WriteLine shtml
fout.close
next
response.write"<SCRIPT language=JavaScript>alert(’成功生成"&page_n+1&"個HTML頁’);this.location.href=’javascript:history.back();’</SCRIPT>"
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>阿裏西西_超長文件分頁並生成HTML文件簡單示例</title>
</head>
<body>
<form name="form1" method="post" action="?action=add">
文章內容:
<br>
<textarea name="body" cols="60" rows="8">
第一頁內容
$
第二頁內容
$
第三頁內容</textarea>
<br>
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>
本文版权:http://www.ndfweb.cn/news-288.html
  NDF俱乐部
  国际域名注册
  建站咨询
简体中文 NDF网站建设淘宝店 | ICO图标在线生成 | 外贸网站建设 | 联系我们
©2007-2025 NDF Corporation 鲁ICP备08005967号 Sitemap - RSSRSS订阅