1.filesystemobject实现方法
<%@ LANGUAGE="VBSCRIPT" %>
<%option explicit
dim rs,sql,filename,fs,myfile,x,link
Set fs = server.CreateObject("scripting.filesystemobject")
filename = "c:你要保存的文件名.xls"
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
set myfile = fs.CreateTextFile(filename,true)
'这里数据库连接,并且生成查询记录rs(可以更改的记录名rs)
if rs.EOF and rs.BOF then
else
dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine= strLine & x.name & chr(9)
Next
'--将表的列名先写入EXCEL
myfile.writeline strLine
Do while Not rs.EOF
strLine=""
for each x in rs.Fields
strLine= strLine & x.value & chr(9)
next
'--将表的数据写入EXCEL
myfile.writeline strLine
rs.MoveNext
loop
end if
%>
当然你也可以在(filename = "c:你要保存的文件名.xls")中进行修改,可以
将数据到如到更多的文件格式中,比如.txt文件等!只单单修改以下输出格式就可以了!(如果还要扩展开去的话,你还可以直接从这些文件中读出数据,写入数据库!
2.ContentType = "application/vnd.ms-excel"实现方法
<%
Response.ContentType = "application/vnd.ms-excel"
'这里数据库连接,并且生成查询记录RS(可以更改的记录名RS)
%>
<TABLE BORDER=1>
<TR>
<%
j = 2
For i = 0 to RS.Fields.Count - 1
%>
<TD><B><% = RS(i).Name %></B></TD>
<% Next %>
<TD><B>On Hand (calculated)</B></TD>
<TD><B>Gross (calculated)</B></TD>
</TR>
<%
Do While Not RS.EOF
%>
<TR>
<% For i = 0 to RS.Fields.Count - 1
%>
<TD VALIGN=TOP><% = RS(i) %></TD>
<% Next %>
<TD>=b<%=j%>-c<%=j%>-d<%=j%></TD>
<TD>=d<%=j%>*e<%=j%></TD>
</TR>
<%
RS.MoveNext
j = j + 1
Loop
RS.Close
%>
<TR BGCOLOR=RED>
<TD>统计</TD>
<TD>=SUM(B2:B6)</TD>
<TD>=SUM(C2:C6)</TD>
<TD>=SUM(D2:D6)</TD>
<TD>n/a</TD>
<TD>=SUM(F2:F6)</TD>
<TD>=SUM(G2:G6)</TD>
</TABLE>
感谢大家,希望大家提意见,不断完善!呵呵