2个回答
展开全部
ASP将ACCESS数据导出至EXCEL
<script for="window" event="onload">
window.resizeTo(400,300)
</script>
<%
set conn=Server.CreateObject("adodb.connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("../data/xin#tong.mdb")'//////链接数据库
conn.open connstr
dim rs,sql,filename,fs,myfile,x
Set fs=server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename=Server.MapPath(""&request("exit")&".xls")'/////数据表保存的文件名
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile=fs.CreateTextFile(filename,true)
'///////从数据库中把你想放到EXCEL中的数据查出来
Set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from "&request("exit")&""
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
Response.Write "<BR><BR><center><b>导出成功,请选择继续操作</b></center>"
response.Write "<table width=90% cellspacing=1 cellpadding=3 align=center>"
Response.Write "<tr align=center> <td>"
response.write ("<font color=green>√</font><a href='"&request("exit")&".xls'>下载</a>") & " <font color= green>√</font><a href=javascript:window.close()>关闭</a>"
Response.Write "</td></tr></table>"
%>
<script for="window" event="onload">
window.resizeTo(400,300)
</script>
<%
set conn=Server.CreateObject("adodb.connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& Server.MapPath("../data/xin#tong.mdb")'//////链接数据库
conn.open connstr
dim rs,sql,filename,fs,myfile,x
Set fs=server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename=Server.MapPath(""&request("exit")&".xls")'/////数据表保存的文件名
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile=fs.CreateTextFile(filename,true)
'///////从数据库中把你想放到EXCEL中的数据查出来
Set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from "&request("exit")&""
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
Response.Write "<BR><BR><center><b>导出成功,请选择继续操作</b></center>"
response.Write "<table width=90% cellspacing=1 cellpadding=3 align=center>"
Response.Write "<tr align=center> <td>"
response.write ("<font color=green>√</font><a href='"&request("exit")&".xls'>下载</a>") & " <font color= green>√</font><a href=javascript:window.close()>关闭</a>"
Response.Write "</td></tr></table>"
%>
展开全部
<input type="submit" name="out_excel" onClick="AutomateExcel();" value="导出到excel" class="notPrint">
<%
if request("action")="word" then
set rs=server.createobject("adodb.recordset")
sql="select * from user_visit_record where item_id='"&session("id")&"'"
rs.open sql,conn,1,3
Set ExcelApp =CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.Add
ExcelBook.WorkSheets(1).cells(1,1).value ="id"
ExcelBook.WorkSheets(1).cells(2,1).value = "用户编号"
ExcelBook.WorkSheets(1).cells(2,2).value = "登陆名"
ExcelBook.WorkSheets(1).cells(2,3).value = "真实姓名"
ExcelBook.WorkSheets(1).cells(2,4).value = "密码"
cnt =3
do while not rs.eof
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("user_name")
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("user_ip")
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("gettime")
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("id")
rs.movenext
cnt = cint(cnt) + 1
loop
Excelbook.SaveAs "d:\yourfile.xls" '这个是数据导出完毕以后在D盘存成文件
ExcelApp.Application.Quit '导出以后退出Excel
Set ExcelApp = Nothing '注销Excel对象
end if
%>
<%
if request("action")="word" then
set rs=server.createobject("adodb.recordset")
sql="select * from user_visit_record where item_id='"&session("id")&"'"
rs.open sql,conn,1,3
Set ExcelApp =CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.Add
ExcelBook.WorkSheets(1).cells(1,1).value ="id"
ExcelBook.WorkSheets(1).cells(2,1).value = "用户编号"
ExcelBook.WorkSheets(1).cells(2,2).value = "登陆名"
ExcelBook.WorkSheets(1).cells(2,3).value = "真实姓名"
ExcelBook.WorkSheets(1).cells(2,4).value = "密码"
cnt =3
do while not rs.eof
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("user_name")
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("user_ip")
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("gettime")
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("id")
rs.movenext
cnt = cint(cnt) + 1
loop
Excelbook.SaveAs "d:\yourfile.xls" '这个是数据导出完毕以后在D盘存成文件
ExcelApp.Application.Quit '导出以后退出Excel
Set ExcelApp = Nothing '注销Excel对象
end if
%>
参考资料: http://www.zhangshiju.com/2011/11/06108/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |