asp 把表格导出excel表源码
发布网友
发布时间:2023-07-04 03:46
我来回答
共1个回答
热心网友
时间:2023-10-13 17:10
一、用Excel对象,但要有Excel模板。速度略慢
<!--#include file="../conn.asp"-->
<%
dim s,sql,filename,fs,myfile,x
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)
strSql = "select djh,bmmc,jihua,mubiao from scheme "
Set rstData =conn.execute(strSql)
if not rstData.EOF and not rstData.BOF then
dim trLine,responsestr
strLine=""
For each x in rstData.fields
strLine = strLine & x.name & chr(9)
Next
'--将表的列名先写入EXCEL
myfile.writeline strLine
Do while Not rstData.EOF
strLine=""
for each x in rstData.Fields
strLine = strLine & x.value & chr(9)
next
myfile.writeline strLine
rstData.MoveNext
loop
end if
Response.Write "生成EXCEL文件成功,点击<a href=""users.xls"" target=""_blank"">下载</a>!"
rstData.Close
set rstData = nothing
Conn.Close
Set Conn = nothing
%>
二、生成假的Excel,文件本身的格式是TXT,但扩展名使用的是XLS,用Excel也可以打开。速度快
<!--#include file="../conn.asp"-->
<%
Response.ContentType="application/vnd.ms-excel "
%>
<table border="1" style="border-collapse:collapse;">
<tr align="center">
<td>编号</td>
<td>日期</td>
<td>部门</td>
<td>工作计划</td>
<td>工作目标</td>
</tr>
<%
set rs=server.createobject("adodb.recordset")
sql="select djh,rq,bmmc,jihua,mubiao from scheme where bmmc='"&session("bmmc")&"'"
rs.open sql,conn,1,1
do while not rs.eof
%>
<tr>
<td><%=rs("djh")%></td>
<td><%=rs("rq")%></td>
<td><%=rs("bmmc")%></td>
<td><%=rs("jihua")%></td>
<td><%=rs("mubiao")%></td>
</tr>
<%
rs.movenext
loop
%>
</table>
<%
rs.close
Conn.Close
Set Conn = nothing
%>