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

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 comingtoexpert 的 blog

以下文章来自于http://www.dalianit.com/edu/|78|107|86|121|79|120|62|62|.html,在此向原作者表示感谢!

阿余常要把各种各样的查询结果输出到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">

本文关键:输出EXCEL文件的通用函数,很实用
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top