ndfweb.cn

ASP+FSO+框架实现ASP批量生成HTML并分页的方法


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


<!--#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网站建设淘宝店 | 实用工具 | 外貿網站建設 | 联系我们
鲁公网安备 37110202000336号 鲁ICP备2021027697号-1 Sitemap - RSSRSS订阅