sql1="select * from apply " '这里是要输出EXCEL的查询语句,如 "SESECT * FORM CAI WHERE 性别='女'" filename="excel4.xls" ' 要输出的EXCEL文件的文件名, 你只要改以上两句就行了,其它的都不要改.
call toexcel(FILENAME,sql1) set conn=nothing
function ReadText(FileName) '这是一个用于读出文件的函数 'response.write server.MapPath(FileName) 'response.end 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") 'Response.Write sql 'response.end rs.Open sql,objconn,1,1 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 rs.close 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=UTF-8"> <title>正在生成EXCEL文件</title> </head> <BODY> 正在生成EXCEL文件.... </BODY> </HTML> 答: EXEC master..xp_cmdshell 'bcp "SELECT * FROM 库名.dbo.apply where 性别=女" queryout "excel4.xls" -c -q -S"服务器名" -U"用户名" -P"密码"'