把我开发的WEB版编码系统导出XLS的代码贴出来吧,你参照着修改一下就可以实现你要的功能。
在其他页面用location.href(这样不会新开窗口)调用它:导出
<%
Response.Buffer = True
Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "No-Cache"
Response.Clear
Dim MySql
Dim Rs,Sql
Dim objFSO,xlsFile,objStream
Dim FileName
Dim XlsSize,XlsName
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
FileName = Server.MapPath(".") & "\" & Replace(objFSO.GetTempName,".tmp","") & ".xls"
If objFSO.FileExists(FileName) Then
objFSO.DeleteFile(FileName)
End If
Set xlsFile = objFSO.CreateTextFile(FileName,true)
xlsFile.WriteLine "图号" & CHR(9) & "原图号" & CHR(9) & "规格" & CHR(9) & "名称" & CHR(9) & "英文名称" & CHR(9) & "材料" & CHR(9) & "设计" & CHR(9) & "设计日期" & CHR(9) & "校对" & CHR(9) & "审核" & CHR(9) & "工艺审查" & CHR(9) & "标准化" & CHR(9) & "批准" & CHR(9) & "批准日期" & CHR(9) & "图幅" & CHR(9) & "比例" & CHR(9) & "来源" & CHR(9) & "图样标记" & CHR(9) & "重量" & CHR(9) & "编码人" & CHR(9) & "编码日期" & CHR(9) & "编码时间" & CHR(9) & "编码状态" & CHR(9) & "任务组名" & CHR(9) & "说明" & CHR(9) & "Windchill用否"
Call OpenConn
MySql=Request("sql")
MySql=Replace(Replace(Replace(MySql,"[gt;]",">"),"[lt;]","<"),"CHR(37)","%")
Set Rs=Server.CreateObject("ADODB.RecordSet")
Sql="Select * From [bm] "&MySql
Rs.Open Sql,Conn,1,1
Do While Not Rs.EOF
xlsFile.WriteLine Rs("图号") & CHR(9) & Rs("原图号") & CHR(9) & Rs("规格") & CHR(9) & Rs("名称") & CHR(9) & Rs("英文名称") & CHR(9) & Rs("材料") & CHR(9) & Rs("设计") & CHR(9) & Rs("设计日期") & CHR(9) & Rs("校对") & CHR(9) & Rs("审核") & CHR(9) & Rs("工艺审查") & CHR(9) & Rs("标准化") & CHR(9) & Rs("批准") & CHR(9) & Rs("批准日期") & CHR(9) & Rs("图幅") & CHR(9) & Rs("比例") & CHR(9) & Rs("来源") & CHR(9) & Rs("图样标记") & CHR(9) & Rs("重量") & CHR(9) & Rs("编码人") & CHR(9) & Rs("编码日期") & CHR(9) & Rs("编码时间") & CHR(9) & Rs("编码状态") & CHR(9) & Rs("任务组名") & CHR(9) & Rs("说明") & CHR(9) & Rs("wt_used")
Rs.MoveNext
Loop
Rs.Close
Set Rs=Nothing
Call CloseConn
Set xlsFile = Nothing
Set xlsFile = objFSO.GetFile(FileName)
XlsSize = xlsFile.Size
'XlsName = xlsFile.Name
Set xlsFile = Nothing
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile FileName
Response.AddHeader "Content-Disposition", "attachment; filename=查询结果.xls"
Response.AddHeader "Content-Length", XlsSize
Response.Charset = "UTF-8"
Response.ContentType = "application/vnd.ms-excel"
Response.BinaryWrite objStream.Read
Response.Flush
Response.Clear()
objStream.Close
Set objStream = Nothing
objFSO.DeleteFile FileName
Set objFSO = Nothing
%>
EXCEL中单元格的默认格式是常规,数字前面的0就自动去掉了,要把单元格格式设置成文本格式才行