ASP 读取 生成 导出 execl

asp函数

2010-11-16 09:15

读取 execl
-----------------------------------------------------------------------------------------------------------------------------------
<%
dim conn
set conn=server.createobject("adodb.connection")
conn.open "driver={Microsoft Excel Driver (*.xls)};DBQ="&server.mappath("x.xls") ‘重点

dim rs
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from [Sheet1$]",conn,1,1
%>

<table width="884" height="57" border="0" cellpadding="0" cellspacing="0">
<tr>
    <td align="center"><%=rs(0).name%></td>
    <td align="center"><%=rs(1).name%></td>
    <td align="center"><%=rs(2).name%></td>
    <td align="center"><%=rs(3).name%></td>
    <td align="center"><%=rs(4).name%></td>
</tr>
<%
do while not rs.eof
%>
</p>

   <tr>
    <td align="center"><%=rs(0).value%></td>
    <td align="center"><%=rs(1).value%></td>
    <td align="center"><%=rs(2).value%></td>
    <td align="center"><%=rs(3).value%></td>
    <td align="center"><%=rs(4).value%></td>
</tr>

<%
rs.movenext
loop
rs.close
%>
</table>

导出execl
-----------------------------------------------------------------------------------------------------------------------------
加上头即可! 直接输出表格
<%
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "content-disposition", "attachment; filename = demo.xls"

%>

<table width="884" height="57" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td align="center">标题1</td>
    <td align="center">标题2</td>
    <td align="center">标题3</td>
</tr>
<tr>
    <td align="center">111</td>
    <td align="center">222</td>
    <td align="center">333</td>
</tr>

   <tr>
    <td align="center">444</td>
    <td align="center">555</td>
    <td align="center">666</td>
</tr>
</table>


生成execl
---------------------------------------------------------------------------------------------------------------------------------
<%
     set conn=server.createobject("adodb.connection")
     conn.open connstring
     set rs=server.createobject("Adodb.recordset")
     sql="select * from xxxxxxxx "
     rs.open sql,conn
     do while not rs.eof
          msg=msg & rs("jsid") & chr(9) & rs("jsxm") & vbcrlf
     rs.movenext
    loop

    
   set fso = server.CreateObject("scripting.filesystemobject")  
   filename = Server.MapPath("demo.xls")  
   if fso.FileExists(filename) then  
       fso.DeleteFile(filename)  
   end if

  
   set myfile = fso.CreateTextFile(filename,true)
   myfile.write msg
   myfile.close
   response.Write("OK")
%>