请问怎样在asp页面中实现sqlserver数据库的数据与Excel的.xls文件的转换啊?

zhangqiuxi 2004-04-29 04:30:02
就是将库表中的数据组织后导成xls文件,以及将xls文件的内容导入数据库?有例子最好
...全文
247 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
kincky 2004-06-21
  • 打赏
  • 举报
回复
mark
zhangqiuxi 2004-05-11
  • 打赏
  • 举报
回复
对不起,这么久才来回复~

谢谢大家了,等我再碰到问题再来问~
500sea 2004-04-30
  • 打赏
  • 举报
回复
钢铁工人的例子不好,哪个excel文件只是把纯文本写入txt文件在改变后缀。
这么生成的excel文件,你用微软的软件可能认出来,用其他的软件读这些
excel文件很可能认不出来。应为excel文件不只是给人看的,。更主要的是
当数据源,给其他程序使用。

不信你把钢铁工人的代码生成的excel文件你用记事本打开,里面是txt文件格式。
你在启动excel生成一个excel文件,用记事本打开,看里面显示的时什么。
就明白差异了。
kaipeng2000 2004-04-30
  • 打赏
  • 举报
回复
谢谢风语者
ghlfllz 2004-04-30
  • 打赏
  • 举报
回复
嘻嘻,my0707(积木)比我快一点点
我传的时候还没有呢,我传完了,他已经在排在我前面了
wj253 2004-04-30
  • 打赏
  • 举报
回复
帮你顶
ghlfllz 2004-04-30
  • 打赏
  • 举报
回复
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<TITLE>生成EXCEL文件</TITLE>
<a href="MakeExcel.asp?act=make">在线生成EXCEL</a>
<%
if Request("act")="" then
Response.Write "生成EXCEL文件"
else
connect="driver={SQL Server};server=192.168.0.24;uid=sa;pwd=123;database=SZSM"
set Conn=Server.CreateObject("ADODB.Connection")
Conn.open connect
dim rs,sql,filename,fs,myfile,x
Set fs=server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename=Server.MapPath("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 * from News_Content order by News_Id desc"
rs.Open sql,conn,1,1
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
End If
%>
my0707 2004-04-30
  • 打赏
  • 举报
回复
怎么最近关于这方面的问题这么多啊?昨天刚回个网友的:

<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
Response.Write "生成EXCEL文件"
else
dim conn,strconn
strconn="driver={SQL Server};server=(local);uid=sa;pwd=;database=mytest"
set conn=server.CreateObject("adodb.connection")
conn.Open strconn
dim rs,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("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 * from a_qst order by id desc"
rs.Open sql,conn,1,1
if not rs.EOF then
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
End If
%>
lawdoor 2004-04-30
  • 打赏
  • 举报
回复
以上文件均为转载,有什么异议,请参照相关网站!
lawdoor 2004-04-30
  • 打赏
  • 举报
回复
【ASP操作Excel技术总结】
目录
一、  环境配置
二、  ASP对Excel的基本操作
三、  ASP操作Excel生成数据表
四、  ASP操作Excel生成Chart图
五、  服务器端Excel文件浏览、下载、删除方案
六、  附录

正文
一、  环境配置
服务器端的环境配置从参考资料上看,微软系列的配置应该都行,即:
1.Win9x+PWS+Office
2.Win2000 Professional+PWS+Office
3.Win2000 Server+IIS+Office
目前笔者测试成功的环境是后二者。Office的版本没有特殊要求,考虑到客户机配置的不确定性和下兼容特性,建议服务器端Office版本不要太高,以防止客户机下载后无法正确显示。
服务器端环境配置还有两个偶然的发现是:
1.  笔者开发机器上原来装有金山的WPS2002,结果Excel对象创建始终出现问题,卸载WPS2002后,错误消失。
2.  笔者开发ASP代码喜欢用FrontPage,结果发现如果FrontPage打开(服务器端),对象创建出现不稳定现象,时而成功时而不成功。扩展考察后发现,Office系列的软件如果在服务器端运行,则Excel对象的创建很难成功。
服务器端还必须要设置的一点是COM组件的操作权限。在命令行键入“DCOMCNFG”,则进入COM组件配置界面,选择Microsoft Excel后点击属性按钮,将三个单选项一律选择自定义,编辑中将Everyone加入所有权限。保存完毕后重新启动服务器。
客户端的环境配置没发现什么特别讲究的地方,只要装有Office和IE即可,版本通用的好象都可以。

二、  ASP对Excel的基本操作
1、  建立Excel对象
set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false    不显示警告
objExcelApp.Application.Visible = false    不显示界面
2、  新建Excel文件
objExcelApp.WorkBooks.add
set objExcelBook = objExcelApp.ActiveWorkBook
set objExcelSheets = objExcelBook.Worksheets
set objExcelSheet = objExcelBook.Sheets(1)
3、  读取已有Excel文件
strAddr = Server.MapPath(".")
objExcelApp.WorkBooks.Open(strAddr & "\Templet\Table.xls")
set objExcelBook = objExcelApp.ActiveWorkBook
set objExcelSheets = objExcelBook.Worksheets
set objExcelSheet = objExcelBook.Sheets(1)
4、  另存Excel文件
objExcelBook.SaveAs strAddr & "\Temp\Table.xls"
5、  保存Excel文件
objExcelBook.Save    (笔者测试时保存成功,页面报错。)
6、  退出Excel操作
objExcelApp.Quit  一定要退出
set objExcelApp = Nothing

三、  ASP操作Excel生成数据表
1、  在一个范围内插入数据
objExcelSheet.Range("B3:k3").Value = Array("67", "87", "5", "9", "7", "45", "45", "54", "54", "10")
2、  在一个单元格内插入数据
objExcelSheet.Cells(3,1).Value="Internet Explorer"
3、  选中一个范围
4、  单元格左边画粗线条
5、  单元格右边画粗线条
6、  单元格上边画粗线条
7、  单元格下边画粗线条
8、  单元格设定背景色
9、  合并单元格
10、  插入行
11、  插入列

四、  ASP操作Excel生成Chart图
1、  创建Chart图
objExcelApp.Charts.Add
2、  设定Chart图种类
objExcelApp.ActiveChart.ChartType = 97
注:二维折线图,4;二维饼图,5;二维柱形图,51
3、  设定Chart图标题
objExcelApp.ActiveChart.HasTitle = True
objExcelApp.ActiveChart.ChartTitle.Text = "A test Chart"
4、  通过表格数据设定图形
objExcelApp.ActiveChart.SetSourceData objExcelSheet.Range("A1:k5"),1
5、  直接设定图形数据(推荐)
objExcelApp.ActiveChart.SeriesCollection.NewSeries
objExcelApp.ActiveChart.SeriesCollection(1).Name = "=""333"""
objExcelApp.ActiveChart.SeriesCollection(1).Values = "={1,4,5,6,2}"
6、  绑定Chart图
objExcelApp.ActiveChart.Location 1
7、  显示数据表
objExcelApp.ActiveChart.HasDataTable = True
8、  显示图例
objExcelApp.ActiveChart.DataTable.ShowLegendKey = True

五、  服务器端Excel文件浏览、下载、删除方案
浏览的解决方法很多,“Location.href=”,“Navigate”,“Response.Redirect”都可以实现,建议用客户端的方法,原因是给服务器更多的时间生成Excel文件。
下载的实现要麻烦一些。用网上现成的服务器端下载组件或自己定制开发一个组件是比较好的方案。另外一种方法是在客户端操作Excel组件,由客户端操作服务器端Excel文件另存至客户端。这种方法要求客户端开放不安全ActiveX控件的操作权限,考虑到通知每个客户将服务器设置为可信站点的麻烦程度建议还是用第一个方法比较省事。
删除方案由三部分组成:
A:  同一用户生成的Excel文件用同一个文件名,文件名可用用户ID号或SessionID号等可确信不重复字符串组成。这样新文件生成时自动覆盖上一文件。
B:  在Global.asa文件中设置Session_onEnd事件激发时,删除这个用户的Excel暂存文件。
C:  在Global.asa文件中设置Application_onStart事件激发时,删除暂存目录下的所有文件。
注:建议目录结构 \Src 代码目录 \Templet 模板目录 \Temp 暂存目录

六、  附录
出错时Excel出现的死进程出现是一件很头疼的事情。在每个文件前加上“On Error Resume Next”将有助于改善这种情况,因为它会不管文件是否产生错误都坚持执行到“Application.Quit”,保证每次程序执行完不留下死进程。
lawdoor 2004-04-30
  • 打赏
  • 举报
回复
【输出EXCEL文件的通用函数,很实用】
阿余常要把各种各样的查询结果输出到EXCEL中,所以做了下面这段小程序,用于把一个SQL的SELECT查询出的结果输出为EXCEL格式文件,这个程序你只要设好用于取得一个记录集的SQL的SELECT查询语句和一个文件名,程序就能输出EXCEL格式文件了,这个程序一共由三个文件构成,第一个文件的文件名为:toexcel.asp是主文件,内容如下:

<%

'前面是设置数据源并链接到数据库,请自行书写相关语句,此处略过

sql=session("toexcelsql") '这里是要输出EXCEL的查询语句,如 "SESECT * FORM CAI WHERE 性别='女'"
filename="excel.xls" ' 要输出的EXCEL文件的文件名, 你只要改以上两句就行了,其它的都不要改.

'你只要修改以上两变量就行了.其它的我都做好了.

call toexcel(FILENAME,sql)
set conn=nothing

function ReadText(FileName) '这是一个用于读出文件的函数
set adf=server.CreateObject("Adodb.Stream")
with adf
.Type=2
.LineSeparator=10
.Open
.LoadFromFile (server.MapPath(FileName))
.Charset="GB2312"
.Position=2
ReadText=.ReadText
.Cancel()
.Close()
end with
set ads=nothing
end function

sub SaveText(FileName,Data) '这是一个用于写文件的函数
set fs= createobject("scripting.filesystemobject")
set ts=fs.createtextfile(server.MapPath(FileName),true)
ts.writeline(data)
ts.close
set ts=nothing
set fs=nothing
end sub

sub toexcel(filename,sql) '这是一个根据SQL语句和FILENAME生成EXCEL文件
Set rs=Server.CreateObject("ADODB.RecordSet")
rs.Open sql,conn,1,3
TOEXCELLR="<table width='100%'><tr >"
set myfield=rs.fields
dim fieldname(50)
for i=0 to myfield.count-1
toexcellr=toexcellr&"<td class=xl24>"&MYFIELD(I).NAME&"</td>"
fieldname(i)=myfield(i).name
if myfield(i).type=135 then datename=datename&myfield(i).name&","
next
toexcellr=toexcellr&"</tr>"
do while not rs.eof
toexcellr=toexcellr&"<tr>"
for i=0 to myfield.count-1
if instr(datename,fieldname(i)&",")<>0 then
if not isnull(rs(fieldname(i))) then
TOEXCELLR=TOEXCELLR&"<td class=xl25 ><p align='left'>"&formatdatetime(rs(fieldname(i)),2)&"</p></td>"
else
TOEXCELLR=TOEXCELLR&"<td class=xl25 ><p align='left'> </p></td>"
end if
else
TOEXCELLR=TOEXCELLR&"<td class=xl24 >"&rs(fieldname(i))&"</td>"
end if
next
toexcellr=toexcellr&"</tr>"
rs.movenext
loop
toexcellr=toexcellr&"</table>"
tou=readtext("tou.txt")
di=readtext("di.txt")
toexcellr=tou&toexcellr&di
call savetext(filename,toexcellr)
end sub
%>
<html>
<head>
<meta http-equiv="refresh" content="3;URL=<%=filename%>">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>正在生成EXLCE文件</title>
</head>
<BODY>
正在生成EXLCE文件....
</BODY>
</HTML>

**************第二个文件名为:di.txt 内容如下:

<table x:str border=0 cellpadding=0 cellspacing=0 width=288 style='border-collapse:
collapse;table-layout:fixed;width:216pt'>
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
<td width=72 style='width:54pt'></td>
</tr>
<![endif]>
</table>

************第三个文件的文件名为:tou.TXT 内容如下:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=GB2312">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 9">
<link rel=File-List href="./222.files/filelist.xml">
<link rel=Edit-Time-Data href="./222.files/editdata.mso">
<link rel=OLE-Object-Data href="./222.files/oledata.mso">
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>xky</o:Author>
<o:LastAuthor>xky</o:LastAuthor>
<o:Created>2002-05-27T17:51:00Z</o:Created>
<o:LastSaved>2002-06-22T10:03:03Z</o:LastSaved>
<o:Company>zydn</o:Company>
<o:Version>9.2812</o:Version>
</o:DocumentProperties>
<o:OfficeDocumentSettings>
<o:DownloadComponents/>
<o:LocationOfComponents HRef="file:///E:/msowc.cab"/>
</o:OfficeDocumentSettings>
</xml><![endif]-->
<style>
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:General;
text-align:general;
vertical-align:bottom;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:常规;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
border:.5pt solid windowtext;}
.xl25
{mso-style-parent:style0;
mso-number-format:"Long Date";
text-align:left;
border:.5pt solid windowtext;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Sheet1</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>225</x:DefaultRowHeight>
<x:Print>
<x:ValidPrinterInfo/>
<x:PaperSizeIndex>9</x:PaperSizeIndex>
<x:HorizontalResolution>-3</x:HorizontalResolution>
<x:VerticalResolution>0</x:VerticalResolution>
</x:Print>
<x:Selected/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
<x:ActiveRow>24</x:ActiveRow>
<x:ActiveCol>5</x:ActiveCol>
</x:Pane>
</x:Panes>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
<x:ExcelWorksheet>
<x:Name>Sheet2</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>225</x:DefaultRowHeight>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
<x:ExcelWorksheet>
<x:Name>Sheet3</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>225</x:DefaultRowHeight>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>6600</x:WindowHeight>
<x:WindowWidth>12000</x:WindowWidth>
<x:WindowTopX>0</x:WindowTopX>
<x:WindowTopY>1395</x:WindowTopY>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><![endif]-->
</head>

<body link=blue vlink=purple>

lawdoor 2004-04-30
  • 打赏
  • 举报
回复
【将Excel中的数据导入至MsSQLSERVER中(示例)】
'/*************************************************************************/
'/* EXCEL数据证书导入程序            */
'/* 2003-6-13 yinxiang www.ykce.com            */
'/*************************************************************************/
'/* dataIntoSqlServer_ceritificate(strFileName,strSheetName,myConn,strKind) */
'/* 参数说明: */
'/* strFileName --XLS文件名 */
'/* strSheetName--标签名 */
'/* myConn--外连接 */
'/* strKind--专业代码 */
'/************************************************************************/
sub dataIntoSqlServer_ceritificate(strFileName,strSheetName,myConn,strKind)
'定义
dim myConnection
dim strName
dim rsXsl,rsSql
dim str_Xsl,str_Sql
dim myConn_Xsl
dim cmd
dim i,j
dim strKmId '科目ID号
dim maxId
dim maxOrderId
dim maxKm
dim str_Date
dim str_Kind

strName=strFileName
set myConnection=server.createobject("adodb.connection")
set rsXsl=Server.Createobject("ADODB.Recordset")
set rsSql=Server.CreateObject("ADODB.Recordset")
set cmd=Server.CreateObject("ADODB.Command")
set cmd.ActiveConnection=myConn

'证书种类
str_Kind=split(strKind,"-")

'加入上传日期时间
str_Date=FormatDateTime(Date(),2)& " " & Time()
myConn_Xsl="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strName& ";Extended Properties=Excel 8.0"
'打开连接
myconnection.open myConn_Xsl

'打开表
str_Xsl="select * from ["& strSheetName &"$]"
rsXsl.open str_Xsl,myconnection,1,1

'//姓名,身份证号码,证书号码,签发日期,有效日期
j=1
Do While not rsXsl.eof

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'取出最大值
str_Sql="select Max(id) as maxId from ceritificate"
rsSql.open str_Sql,myConn,1,3
If Not rsSql.Eof Then
If not isNull(rsSql("maxId")) Then
maxId=Clng(rsSql("maxId"))+1
Else
maxId=1
End if
else
maxId=1
End if
rsSql.close'//关闭对象
'加入成绩单
str_Sql=" insert into ceritificate values("&maxId&",'"&rsXsl(0)&"','"&rsXsl(1)&"','"&rsXsl(2)&"','"& str_Kind(0) & "','" & rsXsl(3)& "','"&rsXsl(4) &"','" & str_Date &"')"
cmd.CommandText=str_Sql
cmd.Execute()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
j=j+1
rsXsl.movenext
Loop

response.write "<font color='red'>" & str_Kind(1) & "</font>证书导入成功.<br>"
response.write "共导入<font color='red'>" & j & "</font>条证书信息.<br>"
response.write "<a href=# onclick='self.close();'>关闭窗口</a>"
set rsXsl=nothing
set rsSql=nothing
set myconnection=nothing
set cmd=nothing
end sub

代码说明:
  1)上列代码是将EXCEL中的数据信息导入至SQLSERVER中,strKind参数是指证书的种类;
2) 链接Excel字符串:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strName& ";Extended Properties=Excel 8.0"
3) str_Xsl="select * from ["& strSheetName &"$]"这条语句是确定是Excel哪一个表签,即表

lawdoor 2004-04-30
  • 打赏
  • 举报
回复
【一个将数据库里面的内容生成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');

zhangqiuxi 2004-04-30
  • 打赏
  • 举报
回复
有人知道么?

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧