28,390
社区成员
发帖
与我相关
我的任务
分享
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
'FUNCTION:(TN) QUERY DATA FROM ACCESS DB AND INSERT THEM INTO EXCEL WHICH CREATED NEWLY BY FSO OBJECT
dim msg,exlFileName
msg=""
'exlFileName=trim(request.Form("weFile01")) 'get file name from client
exlFileName="ExcelFileName001"
err.clear
if exlFileName<>"" then
'--------------------------------------------------------------------------------------------
'Connect ACCESS DB and then query data from table
dim acDBName,objDB,strConn
acDBName="DBS/DB01.mdb"
strConn="DRIVER={microsoft access driver (*.mdb)};UID=;PWD=;DBQ="&server.MapPath(acDBName)
set objDB=server.CreateObject("adodb.connection")
objDB.open strConn
dim objRS,qSQL
qSQL="select * from pnAddress"
set objRS=server.CreateObject("adodb.recordset")
objRS.open qSQL,objDB,1,1
'--------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------
'Create EXCEL file and write (query)data to it
if objRS.bof and objRS.eof then
msg="<font color=""red"">No data in ACCESS DB, and not create EXCEL file.</font>"
else
dim objFSO,objExcel,saveExlFileName,strTemp,tempA,i
'create EXCEL file name include path
saveExlFileName=""
saveExlFileName="Excels/"&exlFileName&year(now()) '可以使用程序生成保存的路径或文件夹
if len(month(now()))<2 then
saveExlFileName=saveExlFileName&"0"&month(now())
else
saveExlFileName=saveExlFileName&month(now())
end if
if len(day(now()))<2 then
saveExlFileName=saveExlFileName&"0"&day(now())
else
saveExlFileName=saveExlFileName&day(now())
end if
if len(hour(now()))<2 then
saveExlFileName=saveExlFileName&"0"&hour(now())
else
saveExlFileName=saveExlFileName&hour(now())
end if
if len(minute(now()))<2 then
saveExlFileName=saveExlFileName&"0"&minute(now())
else
saveExlFileName=saveExlFileName&minute(now())
end if
saveExlFileName=saveExlFileName&".xls"
on error resume next
'create FSO and create EXCEL file, exist then delete and create it
set objFSO=server.CreateObject("scripting.FileSystemObject")
if objFSO.fileExists(server.MapPath(saveExlFileName)) then
objFSO.deleteFile(server.MapPath(saveExlFileName))
end if
set objExcel=objFSO.createTextFile(server.MapPath(saveExlFileName),true,true)
'以覆盖(第一个true)和以unicode(第二个true)编码方式建立文件,有中文内容所以需要以unicode方式建立文件
'create HEAD of EXCEL file, HEAD data query from ACCESS DB
for i=0 to objRS.fields.count -1
tempA=tempA & objRS.fields(i).name & chr(9) 'chr(9) 为制表符 Tab 键
next
objExcel.write tempA & chr(13) 'Atten: make a enter in the end of line, 每行完注意回车
'create BODY of EXCEL file, BODY data query from ACCESS DB
do while not objRS.eof
tempA=""
for i=0 to objRS.fields.count-1
tempA=tempA & objRS.fields(i).value & chr(9)
next
objExcel.write tempA 'write a recod, 写入一条记录
objExcel.write chr(13) 'Atten: make a enter in the end of line, 每行完注意回车
objRS.movenext
loop
'close OBJECT and release system source
objExcel.close
set objExcel=nothing
'objFSO.close 'Atten: (TN)带开的文件对象没有 close 方法, 可以直接设置为空,
set objFSO=nothing
msg="ok"
end if
'--------------------------------------------------------------------------------------------
objRS.close
set objRS=nothing
objDB.close
set objDB=nothing
else
msg="<font color=""red"">File name is empty and not create EXCEL file.</font>"
end if
'Display error information if go wrong
if err.number<>0 then
response.Write "<br><font color=""red"">Error code:</font> " & err.number
response.Write "<br><font color=""red"">Error description:</font> " & err.description
response.Write "<br><font color=""red"">Error source:</font> " & err.source
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>FSO建立并增加数据到EXCEL中</title>
</head>
<style type="text/css">
body,td,div,input,fieldset,textarea{font-family:Verdana; font-size:11px; color:#333333; font-weight:normal;}
a:link,a:visited{font-family:Verdana; font-size:11px; color:#333333; font-weight:normal; padding:0px 3px; line-height:20px; text-decoration:none;}
a:hover,a:active{font-family:Verdana; font-size:11px; color:#FF6600; font-weight:normal;}
span{font-family:Verdana; font-size:11px; color:red; font-weight:bold; padding-left:5px; margin:0px 10px;}
</style>
<body>
<p> </p>
<p>
<%
if msg="ok" then
response.Write "<b>OK! EXCEL FILE CREATED:</b>"
response.Write "<hr size=""1"" color=""#FF6600"">"
response.Write "Click and download <a href=""" & saveExlFileName & """><b>" & saveExlFileName & "</b></a>"
else
response.Write "<b>Tip:</b><br>" & msg
end if
%>
</p>
<br><br>
<div id="tip" style="text-align:right;"><a href="excels"><b>EXCEL文件管理</b></a></div>
</body>
</html>