Asp生成Excel文件示例

Asp生成Excel文件示例

Asp生成Excel文件示例
代码如下:
 

&lt;HTML&gt;<br />&lt;HEAD&gt;<br />&lt;meta&nbsp;content=&quot;text/html;&nbsp;charset=gb2312&quot;&nbsp;http-equiv=&quot;Content-Type&quot;&gt;&nbsp;<br />&lt;TITLE&gt;生成EXCEL文件&lt;/TITLE&gt;&nbsp;<br />&lt;/HEAD&gt;&nbsp;<br />&lt;body&gt;&nbsp;<br />&lt;a&nbsp;href=&quot;dbtoexcel.asp?act=make&quot;&gt;在线生成EXCEL&lt;/a&gt;&nbsp;<br />&lt;hr&nbsp;size=1&nbsp;align=left&nbsp;width=300px&gt;&nbsp;<br />&lt;%<br />if&nbsp;Request(&quot;act&quot;)&nbsp;=&nbsp;&quot;&quot;&nbsp;then&nbsp;<br />Response.Write&nbsp;&quot;生成EXCEL文件&quot;&nbsp;<br />else<br />dim&nbsp;conn,strconn&nbsp;<br />strconn=&quot;driver={SQL&nbsp;Server};server=wen;uid=sa;pwd=;database=DB_Test&quot;&nbsp;<br />set&nbsp;conn=server.CreateObject(&quot;adodb.connection&quot;)&nbsp;<br />conn.Open&nbsp;strconn<br />dim&nbsp;rs,sql,filename,fs,myfile,x<br />Set&nbsp;fs&nbsp;=&nbsp;server.CreateObject(&quot;scripting.filesystemobject&quot;)&nbsp;<br />'--假设你想让生成的EXCEL文件做如下的存放&nbsp;<br />filename&nbsp;=&nbsp;Server.MapPath(&quot;online.xls&quot;)&nbsp;<br />'--如果原来的EXCEL文件存在的话删除它&nbsp;<br />if&nbsp;fs.FileExists(filename)&nbsp;then<br />fs.DeleteFile(filename)&nbsp;<br />end&nbsp;if<br />'--创建EXCEL文件<br />set&nbsp;myfile&nbsp;=&nbsp;fs.CreateTextFile(filename,true)<br />Set&nbsp;rs&nbsp;=&nbsp;Server.CreateObject(&quot;ADODB.Recordset&quot;)<br />'--从数据库中把你想放到EXCEL中的数据查出来&nbsp;<br />sql&nbsp;=&nbsp;&quot;select&nbsp;*&nbsp;from&nbsp;Tb_Execl&nbsp;order&nbsp;by&nbsp;id&nbsp;desc&quot;&nbsp;<br />rs.Open&nbsp;sql,conn,1,1<br /><br />if&nbsp;rs.EOF&nbsp;and&nbsp;rs.BOF&nbsp;then<br />else<br />dim&nbsp;strLine,responsestr&nbsp;<br />strLine=&quot;&quot;<br />For&nbsp;each&nbsp;x&nbsp;in&nbsp;rs.fields<br />strLine&nbsp;=&nbsp;strLine&nbsp;&amp;&nbsp;x.name&nbsp;&amp;&nbsp;chr(9)<br />Next<br />'--将表的列名先写入EXCEL&nbsp;<br />myfile.writeline&nbsp;strLine<br />Do&nbsp;while&nbsp;Not&nbsp;rs.EOF&nbsp;<br />strLine=&quot;&quot;<br />for&nbsp;each&nbsp;x&nbsp;in&nbsp;rs.Fields&nbsp;<br />strLine&nbsp;=&nbsp;strLine&nbsp;&amp;&nbsp;x.value&nbsp;&amp;&nbsp;chr(9)&nbsp;<br />next<br />'--将表的数据写入EXCEL&nbsp;<br />myfile.writeline&nbsp;strLine<br />rs.MoveNext<br />loop<br />end&nbsp;if<br />rs.Close&nbsp;<br />set&nbsp;rs&nbsp;=&nbsp;nothing<br />End&nbsp;If<br />%&gt;

Asp生成Excel文件示例

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据