设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP

输出EXCEL文件的通用函数,很实用

作者:佚名 出处:网络转载 时间:12-02 点击:

内容载入中...
 

阿余常要把各种各样的查询结果输出到EXCEL中,所以做了下面这段小程序,用于把一个SQL的SELECT查询出的结果输出为EXCEL格式文件,这个程序你只要设好用于取得一个记录集的SQL的SELECT查询语句和一个文件名,程序就能输出EXCEL格式文件了,这个程序一共由三个文件构成,第一个文件的文件名为:toexcel.asp是主文件,内容如下:

<%

'前面是和来链接到数据库,请自行书写相关语句,此处略过

sql=session("toexcelsql")  '这里是要输出EXCEL的查询语句,如 "SESECT * FORM CAI WHERE 性别='女'"
filename="excel.xls"   ' 要输出的EXCEL文件的文件名, 你只要改以上两句就行了,其它的都不要改.

'你只要修改以上两变量就行了.其它的我都做好了.

call toexcel(FILENAME,sql)
set conn=nothing

function ReadText(FileName)  '这是一个用于读出文件的函数
 set adf=server.CreateObject("Adodb.Stream")
 with adf
  .Type=2
  .LineSeparator=10
  .Open
  .LoadFromFile (server.MapPath(FileName))
  .Charset="GB2312"
  .Position=2
  ReadText=.ReadText
  .Cancel()
  .Close()
 end with
 set ads=nothing
end function

sub SaveText(FileName,Data)  '这是一个用于写文件的函数
 set fs= createobject("scripting.filesystemobject")
    set ts=fs.createtextfile(server.MapPath(FileName),true)
    ts.writeline(data)
    ts.close
    set ts=nothing
    set fs=nothing
end sub

sub toexcel(filename,sql)  '这是一个根据SQL语句和FILENAME生成EXCEL文件
  Set rs=Server.CreateObject("ADODB.RecordSet")
  rs.Open sql,conn,1,3
  TOEXCELLR="<table width='100%'><tr >"
  set myfield=rs.fields
  dim fieldname(50)
  for i=0 to myfield.count-1
     toexcellr=toexcellr&"<td class=xl24>"&MYFIELD(I).NAME&"</td>"
     fieldname(i)=myfield(i).name
     if myfield(i).type=135 then datename=datename&myfield(i).name&","
  next
  toexcellr=toexcellr&"</tr>"
  do while not rs.eof
    toexcellr=toexcellr&"<tr>"
    for i=0 to myfield.count-1
      if instr(datename,fieldname(i)&",")<>0 then
        if not isnull(rs(fieldname(i))) then
           TOEXCELLR=TOEXCELLR&"<td  class=xl25 ><p align='left'>"&formatdatetime(rs(fieldname(i)),2)&"</p></td>"
        else
           TOEXCELLR=TOEXCELLR&"<td  class=xl25 ><p align='left'> </p></td>"
        end if
      else
         TOEXCELLR=TOEXCELLR&"<td class=xl24 >"&rs(fieldname(i))&"</td>"
      end if
    next
    toexcellr=toexcellr&"</tr>"
    rs.movenext
  loop
  toexcellr=toexcellr&"</table>"
  tou=readtext("tou.txt")
  di=readtext("di.txt")
  toexcellr=tou&toexcellr&di
  call savetext(filename,toexcellr)
end sub
%>
<html>
<head>
<meta http-equiv="refresh" content="3;URL=<%=filename%>">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>正在生成EXLCE文件</title>
</head>
<BODY>
正在生成EXLCE文件....
</BODY>
</HTML>

**************第二个文件名为:di.txt 内容如下:

<table x:str border=0 cellpadding=0 cellspacing=0 width=288 style='border-collapse:
 collapse;table-layout:fixed;width:216pt'>
 <![if supportMisalignedColumns]>
 <tr height=0 style='display:none'>
  <td width=72 style='width:54pt'></td>
  <td width=72 style='width:54pt'></td>
  <td width=72 style='width:54pt'></td>
  <td width=72 style='width:54pt'></td>
 </tr>
 <![endif]>
</table>

||||||************第三个文件的文件名为:tou.TXT 内容如下:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=GB2312">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 9">
<link rel=File-List href="./222.files/filelist.xml">
<link rel=Edit-Time-Data href="./222.files/editdata.mso">
<link rel=OLE-Object-Data href="./222.files/oledata.mso">
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
document.getElementById('loading').style.display="none";

收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息