CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  ASP

在ASP中,如何把数据库的数据写到Excel.

楼主nfenghklibra(血雾冥蝶)2004-12-03 08:58:16 在 Web 开发 / ASP 提问

ASP中,我想把查询的记录集写到Excel里.请求代码相助。谢谢! 问题点数:20、回复次数:6Top

1 楼bzscs(沙虫 我爱小美)回复于 2004-12-03 09:03:36 得分 20

一个将数据库里面的内容生成EXCEL的例子  
   
  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');  
   
   
  Top

2 楼nfenghklibra(血雾冥蝶)回复于 2004-12-03 10:37:25 得分 0

数据可以导出了,但不是本地用户机器,而是在保存在服务器,能否有个弹出个下载的对话框。Top

3 楼masm_silly(我讨厌编程)回复于 2004-12-03 11:20:47 得分 0

非常简单,加以下几个语句:  
  Response.Buffer   =   True  
  Response.AddHeader   "Content-Type","application/vnd.ms-excel;   charset=gb2312"  
  Response.AddHeader   "Content-Disposition","attachment;filename=aaa.xls"Top

4 楼comszsoft(星星点灯)回复于 2004-12-03 11:27:15 得分 0

强烈关注Top

5 楼comszsoft(星星点灯)回复于 2004-12-03 11:28:15 得分 0

楼上的应景解决了,  
  Response.Buffer   =   True  
  Response.AddHeader   "Content-Type","application/vnd.ms-excel;   charset=gb2312"  
  Response.AddHeader   "Content-Disposition","attachment;filename=aaa.xls"  
  就是这个Top

6 楼nfenghklibra(血雾冥蝶)回复于 2004-12-03 11:36:48 得分 0

这个加在那地方,是弹出个下载的对话框了。可是没有数据出来。我发代码给看看。  
  <%  
  dim   con1  
  dim   res1,sql1,filename,fs1,myfile1,x,link1  
  dim   strLine1,responsestr1  
  set   conn1=server.CreateObject("ADODB.Connection")  
  Set   res1=Server.CreateObject("ADODB.RecordSet")  
  Set   fs1=server.CreateObject("scripting.filesystemobject")  
  con1=Application("SystemData_ConnectionString")  
  '--假设你想让生成的EXCEL文件做如下的存放  
  Response.Buffer   =   True  
  Response.AddHeader   "Content-Type","application/vnd.ms-excel;   charset=gb2312"  
  Response.AddHeader   "Content-Disposition","attachment;filename=online.xls"  
  filename="c:\online.xls"  
  '--如果原来的EXCEL文件存在的话删除它  
  if   fs1.FileExists(filename)   then  
  fs1.DeleteFile(filename)  
  end   if  
  '--创建EXCEL文件  
  set   myfile1=   fs1.CreateTextFile(filename,true)  
  '--从数据库中把你想放到EXCEL中的数据查出来  
  sql1="select   *   from   departments   "  
  res1.CursorType=3  
  res1.open   sql1,con1  
  if   res1.EOF   and   res1.BOF   then  
   
  else  
  strLine1=""  
  For   each   x   in   res1.fields  
  strLine1=strLine1   &   x.name   &   chr(9)  
  Next  
  '--将表的列名先写入EXCEL  
  myfile1.writeline   strLine1  
  Do   while   Not   res1.EOF  
  strLine1=""  
  for   each   x   in   res1.Fields  
  strLine1=strLine1   &   x.value   &   chr(9)  
  next  
  '--将表的数据写入EXCEL  
  myfile1.writeline   strLine1  
  res1.MoveNext  
  loop  
  end   if  
  res1.Close  
  set   res1=nothing  
  'con1.close  
  set   con1=nothing  
  set   myfile=nothing  
  Set   fs=Nothing  
  %>  
  Top

相关问题

  • asp 数据库
  • ASP与数据库
  • ASP+SQL数据库
  • asp连数据库
  • 用asp怎样实现将数据库导进Excel电子表格和Excel电子表格导进数据库!?
  • 导入EXCEL进数据库!
  • Excel导入sql数据库
  • 如何把access数据库的内容用asp导到excel中?
  • 关于asp数据库在线导出excel文件问题
  • 急!!!!!!asp怎样连接excel的表格数据库,作为网页的数据库?

关键词

  • excel

得分解答快速导航

  • 帖主:nfenghklibra
  • bzscs

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo