<p>还是第一次在CSDN写文章,本人文采和理论知识有限,写得不正确的地方欢迎指正。其实网上已经有很多ASP生成htm的文章了<br /> ,有一种方法是ASP+XML的生成方法,虽然有一种好处就是不用程序写模版就可以直接引用原来的要生成页面源码使用,但<br /> 本人进行此方法测试时,发现其稳定性差和网速要求高(当然不是在服务器上了)。特别是虚拟空间上经常报错,有可能在本<br /> 人在此方法上代码处理不足的原因吧。长话短说,这篇文章使用大家常用的另一种方法ASP+FSO,这里还应用了框架就是为<br /> 了处理大量分页时减少生成时间使用的,这种方法是针对一些页面量较大的ASP文件。</p> <p>这里我引用一个简单实例:(旅游电子商务)全国各大城市酒店应用静态页(htm)分页显示</p> <p>1.应用系统环境:win2000+ASP+MSSQL/ACCESS(数据库基本没有关系了通用的)+iis5.0</p> <p>2.1个城市列表(CityHtml):包括定义静态htm名称共三个字段(城市ID(自动编号),城市名称(CityName例如北京),生成htm前缀名(HtmlStartName例如beijing))</p> <p>3.1个全国酒店列表(Hotel):这里我只建立三个字段(酒店ID(自动编号),城市名称(City),酒店名称(HotelName))方便来引用实例。</p> <p>4.1个ASP页面(ToHtm.asp)(生成htm使用)</p> <p>5.1个循环框架页面(IframeToHtm.asp),应用框架批量生成htm</p> <p>以下给出两个页面的源码</p> <p>循环框架进行批量生成的页面:IFrameToHtm.asp</p> <p>&lt;!--#include file=&quot;conn.asp&quot;--&gt;&#39;连接数据库<br /> &lt;%<br /> dim rs,sql,j<br /> set rs=Server.CreateObject(&quot;adodb.recordset&quot;)<br /> sql=&quot;select * from CityHtml&quot;&#39;打开全国城市列表<br /> rs.open sql,conn,1,1<br /> do until rs.eof&#39;循环各大城市%&gt;<br /> &lt;!--以下应用框架打开ToHtml生成页面--&gt;<br /> &lt;IFRame name=&quot;LoadRcHtm&lt;%=j%&gt;&quot; frameborder=0 width=100% height=30 scrolling=no src=&quot;ToHtml.asp?City=&lt;%=cstr(rs(&quot;city&quot;))%&gt;&amp;HtmlStartName=&lt;%=rs(&quot;HtmlStart&quot;)%&gt;&quot;&gt;&lt;/IFrame&gt;</p> <p>&lt;%rs.movenext<br /> loop%&gt;</p> <p>生成程序页面:ToHtm.asp 我在源码大概写上注释**</p> <p>&lt;!--#include file=&quot;conn.asp&quot;--&gt;&#39;数据连接文件<br /> &lt;%<br /> On Error Resume Next&#39;容错处理<br /> Dim City&#39;定义取得要生成页面的城市<br /> City=Request.Querystring(&quot;City&quot;)&#39;获取生成的城市酒店值从框架传过来的在后面将介绍<br /> HtmlStartName=Request.Querystring(&quot;HtmlStartName&quot;)&#39;获得生成htm文件名前缀<br /> Dim sql&#39;搜索字符串,这里我就直接打开表不用搜索变量了,搜索条件按自己写就可以<br /> sql=&quot;select * from Hotel where [City] = &#39;&quot; &amp; City &amp; &quot;&#39; &quot;<br /> Dim oRs&#39;数据操作对象<br /> Dim PageCounts&#39;实现分页生成必须得知呀有多少页<br /> Set oRs = Server.CreateObject(&quot;ADODB.Recordset&quot;)<br /> oRs.Open Sql,oConn,1,1&#39;找开酒店等于City变量的表<br /> oRs.pagesize=10&#39;十个记录为一页<br /> PageCounts=oRs.pagecount&#39;得出要生成多少个页面,循环生成使用<br /> Dim fs&#39;定义fso文件对象<br /> Dim folders&#39;存放生成静态页的文件夹名称<br /> Dim Filestart&#39;定义生成htm文件前缀<br /> Set fs=Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)<br /> Dim i<br /> for i=1 to PageCounts&#39;开始循环生成页面,也就是分页生成了<br /> page=i<br /> oRs.absolutepage=i&#39;页码<br /> rowcount=oRs.pagesize&#39;当页记录数<br /> folders=server.mappath(&quot;CityHtml&quot;)<br /> if (fs.FolderExists(folders)) then&#39;判断文件夹是否存在<br /> else<br /> fs.CreateFolder(folders)&#39;不存在则创建CityHtml文件夹<br /> end if<br /> if i=1 then<br /> Filestart=HtmlStartName&#39;如果为第一页则定义文件名为传值名.例如beijing则为beijing.htm<br /> else<br /> Filestart=HtmlStartName&amp;i&#39;如果第二页则为beijing+1例如有两页也就是i等于2则为 beijing2.htm如此类推...(.htm后缀就在后面加上)<br /> end if<br /> Dim files&#39;定义生成文本文件名称变量<br /> Dim filez&#39;定义文件路径名称变量<br /> files=Filestart&amp;&quot;.txt&quot;&#39;本文件名称<br /> filez=folders&amp;&quot;\&quot;&amp;&quot;files&#39;文本文件路径<br /> &#39;册除文件<br /> Dim checkfile&#39;检查文本文件是否已经存在,是则删除<br /> checkfile=server.mappath(&quot;CityHtml\&quot;&amp;Filestart&amp;&quot;.htm&quot;)&#39;检查htm文件是否已经存在,是则删除<br /> if (fs.FileExists(checkfile)) then&#39;检查htm文件是否已经存在,是则删除<br /> Dim df&#39;定义文件对象*删除文件使用*<br /> Set df=fs.GetFile(checkfile)&#39;定义要册除的文件<br /> df.delete&#39;册除文件<br /> end if&#39;判断结束<br /> Dim ts&#39;定义写入文件对象<br /> set ts = fs.createtextfile(filez,true) &#39;开启写入文件内容**我在正文只简单写入酒店名称和静态数字分页显示**<br /> ts.write(&quot;&lt;Html&gt;&lt;Head&gt;&lt;Title&gt;生成&quot;&amp;City&amp;&quot;城市酒店&lt;/Title&gt;&quot;&amp;vbcrlf)&#39;之后就是要生成的正文件内容了跟使用Response.write<br /> ts.write(&quot;&lt;META http-equiv=Content-Type content=text/html; charset=gb2312&gt;&quot;&amp;vbcrlf)<br /> ts.write(&quot;&lt;meta name=keywords content=&quot;&amp;city&amp;&quot;酒店&gt;&quot;&amp;vbcrlf)<br /> ts.write(&quot;&lt;link href=&#39;/Style/style.css&#39; rel=&#39;stylesheet&#39; type=&#39;text/css&#39;&gt;&lt;/head&gt;&lt;body topmargin=0&gt;&quot;&amp;vbcrlf)<br /> ts.Write(&quot;&lt;TABLE WIDTH=760 cellspacing=0 cellpadding=0 align=center&gt;&quot;&amp;vbcrlf&amp;_<br /> &quot;&lt;TR&gt;&lt;TD width=&#39;100%&#39;&gt;&quot;&amp;vbcrlf)<br /> &#39;分页输出开始<br /> &#39;数字分页程序原理在这我就不多说了,不懂的朋友可在网上搜索一下<br /> Dim page&#39;当前页<br /> Dim Page2&#39;数字分页变量<br /> Dim s&#39;数字分页变量<br /> if page=1 then<br /> ts.write (&quot; [首 页] [前一页] &quot;)<br /> else<br /> ts.write (&quot; &lt;a href=&quot;&amp;HtmlStartName&amp;&quot;.htm&quot;&amp;&quot; class=blue&gt;[首 页]&lt;/a&gt; &lt;a href=&quot;&amp;HtmlStartName&amp;Replace(page-1,1,&quot;&quot;)&amp;&quot;.htm&quot;&amp;&quot; class=blue&gt;前一页&lt;/a&gt; &quot;)<br /> end if<br /> page2=(page-(page mod 10))/10<br /> if page2&lt;1 then page2=0<br /> for s=page2*10-1 to page2*10+10<br /> if s&gt;0 then<br /> if s=cint(page) then<br /> ts.write (&quot; &lt;font color=&#39;#000000&#39;&gt;[&quot;&amp; s &amp; &quot;]&lt;/font&gt;&quot;)<br /> else<br /> if s=1 then<br /> ts.write (&quot; &lt;a href=&quot;&amp;HtmlStartName&amp;replace(s,1,&quot;&quot;)&amp;&quot;.htm&quot;&amp;&quot; class=blue&gt;[&quot;&amp; s &amp;&quot;]&lt;/a&gt;&quot;)<br /> else<br /> ts.write (&quot; &lt;a href=&quot;&amp;HtmlStartName&amp;s&amp;&quot;.htm&quot;&amp;&quot; class=blue&gt;[&quot;&amp; s &amp;&quot;]&lt;/a&gt;&quot;)<br /> end if<br /> end if<br /> if s=ors.pagecount then<br /> exit for<br /> end if<br /> end if<br /> next<br /> if cint(page)=ors.pagecount then<br /> ts.write (&quot; [后一页] [尾 页]&quot;)<br /> else<br /> ts.write (&quot; &lt;a href=&quot;&amp;HtmlStartName&amp;page+1&amp;&quot;.htm&quot;&amp;&quot; class=blue&gt;[后一页]&lt;/a&gt; &lt;a href=&quot;&amp;HtmlStartName&amp;ors.pagecount&amp;&quot;.htm&quot;&amp;&quot; class=blue&gt;[尾 页]&lt;/a&gt;&quot;)<br /> end if<br /> ts.write(&quot;&lt;/TD&gt;&lt;/TR&gt;&quot;)<br /> &#39;分页输出结束<br /> do while not ors.eof and rowcount&gt;0 &#39;输出酒店名称<br /> ts.write(&quot;&lt;TR&gt;&lt;TD width=&#39;100%&#39;&gt;&quot;&amp;oRs.Fields(&quot;Chinese_Name&quot;)&amp;&quot;&lt;/TD&gt;&lt;/TR&gt;&quot;&amp;vbcrlf)<br /> oRs.movenext<br /> rowcount=rowcount-1&#39;当页记录数-1 loop<br /> ts.write(&quot;&lt;/Table&gt;&lt;/body&gt;&lt;/html&gt;&quot;&amp;vbcrlf)<br /> ts.close<br /> set ts=nothing &#39;释放对象<br /> Dim EditFile&#39;定义改写文件变量<br /> Set EditFile = fs.GetFile(filez)&#39;设置改写文件对象<br /> EditFile.name= left(EditFile.name,len(EditFile.name)-4)&amp;&quot;.htm&quot; &#39;改写文本文件成htm<br /> next&#39;循环生成结束(分页生成)<br /> set EditFile=nothing &#39;释放对象<br /> set fs=nothing&#39;释放对象<br /> if err.number&lt;&gt;0 then &#39;处理生成错误<br /> Response.write(City&amp;&quot;更新时发生未知错误&lt;A href=ToHtml.asp?City=&quot;&amp;City&amp;&quot;&amp;HtmlName=&quot;&amp;HtmlStartName&amp;&quot;&gt;重新更新&lt;/A&gt;&quot;)<br /> else<br /> Response.Write(City&amp;&quot;酒店更新已完成 &quot;&amp;Now())<br /> end if<br /> %&gt;</p>
T:0.006051s,M:256.06 KB
返回顶部 留言