asp写入excel
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
<%@ LANGUAGE="VBSCRIPT" %>
<%option explicit%>
<%
if Request("act") = "" then
Response.Write "生成EXCEL文件"
else
dim conn,strconn
strconn="driver={SQL Server};server=xxx;uid=sa;pwd=;database=DB_Test" 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 Tb_Execl order by sort desc"
rs.Open sql,conn
if rs.EOF and rs.BOF then
Response.Write "库里暂时没有数据!"
else
dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine= strLine & & 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
end if
%>
<%
dim s,sql,filename,fs,myfile,x,rs
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("users.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 product order by id desc"
rs.Open sql,conn
StartTime = Request("StartTime")
EndTime = Request("EndTime")
StartEndTime = "AddTime between #"& StartTime &" 00:00:00# and #"& EndTime &" 23:59:59#"
strSql = "select * from product order by id desc"
Set rstData =conn.execute(strSql)
if not rstData.EOF and not rstData.BOF then
dim trLine,responsestr
strLine=""
strline = strline & "设备名称" & chr(9) & "设备编码" & chr(9) & "设备型号" & chr(9) & "设备简介" & chr(9) & "配发时间" & chr(9) & "库存情况" & chr(9) & "单价(元)" & chr(9) & "存放地" & chr(9)
'--将表的列名先写入EXCEL
myfile.writeline strLine
Do while Not rstdata.EOF