您的位置:首页技术开发ASP技巧 → 将数据库里面的内容生成EXCEL

将数据库里面的内容生成EXCEL

时间:2004/11/7 4:10:00来源:本站整理作者:蓝点我要评论(0)

1、ASP文件:



<%@ LANGUAGE="VBSCRIPT" %>

<%option explicit%>

<%

'EXAMPLE AS:把数据库中一个每天24小时在线人数放到一个EXCEL文件中去

'AUTHOR    :钢铁工人

'EMAIL     :hello_hhb@21cn.com

'DATE      :2001-3-25

'TEST      :在NT4,SP6,SQL SERVER 7.0,EXCEL2000中测试通过

%>







生成EXCEL文件





生成在线人口的EXCEL




<%

if Request("act") = "" then

else



  dim conn

  set conn=server.CreateObject("adodb.connection")

  conn.Open "test","sa",""

  'conn.Open Application("connstr")



  dim rs,sql,filename,fs,myfile,x,link

  

  Set fs = server.CreateObject("scripting.filesystemobject")

  '--假设你想让生成的EXCEL文件做如下的存放

  filename = "c:\online.xls"

  '--如果原来的EXCEL文件存在的话删除它

  if fs.FileExists(filename) then

    fs.DeleteFile(filename)

  end if

  '--创建EXCEL文件

  set myfile = fs.CreateTextFile(filename,true)





  

  Set rs = Server.CreateObject("ADODB.Recordset")

  '--从数据库中把你想放到EXCEL中的数据查出来

  sql = "select population,hourpos,datepos from populationperhour order by datepos,hourpos asc"

  rs.Open sql,conn

  if rs.EOF and rs.BOF then

  

  else

  

    dim strLine,responsestr

    strLine=""

      For each x in rs.fields

        strLine= strLine & x.name & chr(9)

      Next

    

      '--将表的列名先写入EXCEL

      myfile.writeline strLine



      Do while Not rs.EOF

      strLine=""

     

      for each x in rs.Fields

        strLine= strLine & x.value & chr(9)

      next

      '--将表的数据写入EXCEL

      myfile.writeline strLine



      rs.MoveNext

      loop

      

  end if



rs.Close

set rs = nothing

conn.close

set conn = nothing

set myfile = nothing

Set fs=Nothing



link="Open The Excel File"

Response.write link

end if

%>









2、数据库相关:



CREATE TABLE [populationperhour] (

    [population] [int] NOT NULL ,

    [hourpos] [int] NOT NULL ,

    [datepos] [datetime] NOT NULL

);

insert into populationperhour values('936','1','2001-1-11');

insert into populationperhour values('636','2','2001-1-11');

insert into populationperhour values('106','3','2001-1-11');

insert into populationperhour values('177','4','2001-1-11');

insert into populationperhour values('140','5','2001-1-11');

insert into populationperhour values('114','6','2001-1-11');

insert into populationperhour values('94','7','2001-1-11');

insert into populationperhour values('49','8','2001-1-11');

insert into populationperhour values('88','9','2001-1-11');

insert into populationperhour values('215','10','2001-1-11');

insert into populationperhour values('370','11','2001-1-11');

insert into populationperhour values('550','12','2001-1-11');

insert into populationperhour values('629','13','2001-1-11');

insert into populationperhour values('756','14','2001-1-11');

insert into populationperhour values('833','15','2001-1-11');

insert into populationperhour values('923','16','2001-1-11');

insert into populationperhour values('980','17','2001-1-11');

insert into populationperhour values('957','18','2001-1-11');

insert into populationperhour values('812','19','2001-1-11');

insert into populationperhour values('952','20','2001-1-11');

insert into populationperhour values('1379','21','2001-1-11');

insert into populationperhour values('1516','22','2001-1-11');

insert into populationperhour values('1476','23','2001-1-11');

insert into populationperhour values('1291','24','2001-1-11');

insert into populationperhour values('1028','1','2001-1-12');

insert into populationperhour values('687','2','2001-1-12');

insert into populationperhour values('462','3','2001-1-12');

insert into populationperhour values('317','4','2001-1-12');

insert into populationperhour values('221','5','2001-1-12');

insert into populationperhour values('158','6','2001-1-12');

insert into populationperhour values('127','7','2001-1-12');

insert into populationperhour values('81','8','2001-1-12');

insert into populationperhour values('96','9','2001-1-12');

insert into populationperhour values('192','10','2001-1-12');

insert into populationperhour values('380','11','2001-1-12');

insert into populationperhour values('629','12','2001-1-12');

insert into populationperhour values('745','13','2001-1-12');

insert into populationperhour values('964','14','2001-1-12');

insert into populationperhour values('1012','15','2001-1-12');

insert into populationperhour values('1050','16','2001-1-12');

insert into populationperhour values('1135','17','2001-1-12');

insert into populationperhour values('1130','18','2001-1-12');

insert into populationperhour values('894','19','2001-1-12');

insert into populationperhour values('1026','20','2001-1-12');

insert into populationperhour values('1430','21','2001-1-12');

insert into populationperhour values('1777','22','2001-1-12');

insert into populationperhour values('1759','23','2001-1-12');

insert into populationperhour values('1697','24','2001-1-12');








相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 VB.NET 2005编写定时关 Jquery get/post下乱码解决方法 前台gbk gb如何使用数据绑定控件显示数据ASP脚本循环语句ASP怎么提速

人气排行 轻松解决"Server Application Error"和iis"一起学习DataGridView调整列宽用ASP随机生成文件名的函数Jquery get/post下乱码解决方法 前台gbk gbODBC Drivers错误80004005的解决办法返回UPDATE SQL语句所影响的行数的方法用Javascript隐藏超级链接的真实地址两个不同数据库表的分页显示解决方案