分享
 
 
 

将数据库内容生成EXCEL的例子

王朝other·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

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中测试通过

%>

<HTML>

<HEAD>

<meta content="text/html; charset=gb2312" http-equiv="Content-Type">

<TITLE>生成EXCEL文件</TITLE>

</HEAD>

<body>

<a href="dbtoexcel.asp?act=make">生成在线人口的EXCEL</a>

<hr size=1 align=left width=300px>

<%

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="<A HREF=" & filename & ">Open The Excel File</a>"

Response.write link

end if

%>

</BODY>

</HTML>

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');

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有