在ASP中怎么把页面中的数据导出到EXCEL
展开全部
<!--#include file="conn.asp"-->
<HTML>
<HEAD>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<TITLE>生成EXCEL文件</TITLE>
</HEAD>
<body>
<hr size=1 align=left width=300px>
<%
dim rs,sql,filename,fs,myfile,objSpreadsheet,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("online.xls")
'--如果原来的EXCEL文件存在的话就删除
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'Set objSpreadsheet = Server.CreateObject("OWC.Spreadsheet")
'objSpreadsheet.Cells(iRow, iCol).Font.Bold = True
'objSpreadsheet.Cells(iRow, iCol).Font.Italic = False
'objSpreadsheet.Cells(iRow, iCol).Font.Size = 10
'objSpreadsheet.Cells(iRow, iCol).Halignment = 2 '居中
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据读出来
sql = "select * from 表名 order by 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
response.Write("<a href='online.xls'>下载</a> ")
end if
rs.Close
set rs = nothing
%>
<HTML>
<HEAD>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<TITLE>生成EXCEL文件</TITLE>
</HEAD>
<body>
<hr size=1 align=left width=300px>
<%
dim rs,sql,filename,fs,myfile,objSpreadsheet,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("online.xls")
'--如果原来的EXCEL文件存在的话就删除
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'Set objSpreadsheet = Server.CreateObject("OWC.Spreadsheet")
'objSpreadsheet.Cells(iRow, iCol).Font.Bold = True
'objSpreadsheet.Cells(iRow, iCol).Font.Italic = False
'objSpreadsheet.Cells(iRow, iCol).Font.Size = 10
'objSpreadsheet.Cells(iRow, iCol).Halignment = 2 '居中
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据读出来
sql = "select * from 表名 order by 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
response.Write("<a href='online.xls'>下载</a> ")
end if
rs.Close
set rs = nothing
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询