当前位置: 代码迷 >> ASP >> 请问asp如何输出excel文件
  详细解决方案

请问asp如何输出excel文件

热度:116   发布时间:2012-02-25 10:01:48.0
请教asp怎么输出excel文件啊
我知道怎样用fso自定义生成一个excel文件
但我不需要生成文件
而是在客户点击一个连接
连接到一个文件会根据内容生成一个excel文件来下载
就是说这个连接是下载一个excel文件不是在服务器生成一个文件
该怎么做呢

------解决方案--------------------
两个建议
一个在客户端,JS操作生成文件,不过建议数据量小的情况下使用
二个FSO在服务端直接生成,利用向客户端写数据时,同时写EXCEL,点击时下载,不过得定时删EXCEL文件
------解决方案--------------------
<!--#include file= "../conn.asp "-->
<%
'response.write "函数返回为 " & getexcell "select reg_id as 编号,reg_name as 名称,reg_sex as 性别,reg_photo as 照片,reg_age as 年龄,reg_city as 所在市,reg_county as 所在县,reg_reading as 在读学级,reg_school as 学校,reg_grade as 年级,reg_class as 班级,reg_phone as 电话,reg_parents as 家长姓名,reg_unit as 单位,reg_address as 地址,reg_hobby as 爱好,reg_date as 填写时间,reg_view as 是否验证 from registration order by reg_view desc ", "registration.xls "

'功能 根据查询产生excel文件
'参数说明
'strconn 数据库连接字符串
'strsql 查询字符串
'strexcelfilename 产生文件名
'说明 很简单
function getexcell(strsql,strexcelfilename)
on error resume next

'查询数据库
rs.open strsql,conn,3,1

if rs.eof then
getexcell= "err没有数据 "
exit function
end if

'建立文件
set objfso=server.createobject( "scripting.filesystemobject ")
filename = server.mappath(strexcelfilename)
if objfso.FileExists(filename) then
objfso.DeleteFile(filename)
end if
set myfile = objfso.CreateTextFile(filename,true)
getexcell=geterr( "创建EXCEL文件出错 ")
if getexcell <> " " then exit function

'--将表的列名先写入EXCEL
strLine= " "
For each x in rs.fields
strLine= strLine & x.name & chr(9)
Next
myfile.writeline strLine

'--将表的数据写入EXCEL
do while not rs.eof
strLine= " "
for each x in rs.Fields
strLine= strLine & Replace(Replace(Replace(Replace(x.value,chr(9), " "),chr(13), " "), " ", " "),chr(10), " ") & chr(9)
next
myfile.writeline strLine
rs.movenext
loop
getexcell=geterr( "写入EXCEL文件出错 ")
if getexcell <> " " then exit function

'成功
getexcell=filename
'link= " <A HREF= " & strexcelfilename & "> 下载Excel表格 </a> "
link = " <script> window.location.href= ' " & strexcelfilename & " '; </script> "
Response.write link

'关闭对象
rs.Close
set rs = nothing
conn.close
set conn = nothing
set myfile = nothing
Set objfso = Nothing
end function

function geterr(strA)
if err.number <> 0 then
geterr= "err " & strA
exit function
else
geterr= " "
end if
end function

getexcell "select reg_id as 编号,reg_name as 名称,reg_sex as 性别,reg_photo as 照片,reg_age as 年龄,reg_city as 所在市,reg_county as 所在县,reg_reading as 在读学级,reg_school as 学校,reg_grade as 年级,reg_class as 班级,reg_phone as 电话,reg_parents as 家长姓名,reg_unit as 单位,reg_address as 地址,reg_hobby as 爱好,reg_date as 填写时间,reg_view as 是否验证 from registration order by reg_view desc ", "registration.xls "
%>
  相关解决方案