Set rs2=Server.CreateObject(ADODB.recordset)
sql=select * from eq where eqbumen='&dep&' order by eqbumen desc
rs2.open sql,conn,1,1
set objExcel=CreateObject(Excel.Application)
objExcel.Workbooks.Open(server.mappath(\test)&\book1.xlt) '打开Excel模板
objExcel.Sheets(1).select '选中工作页
set sheetActive=objExcel.ActiveWorkbook.ActiveSheet
num=2 '从Excel的第2行开始
Do while not rs2.eof '循环直至数据集中的数据写完
strRange=b#&:k# '设定要填写内容的单元区域
sheetActive.range(strRange).font.size=10 '设定字体大小
sheetActive.range(strRange).WrapText=false '设定文字回卷
sheetActive.range(strRange).ShrinkToFit=true '设定是否自动适应表格单元大小
sheetActive.range(strRange).value=array(rs2(zcid),rs2(eqname),rs2(eqchangjia),rs2(eqxinghao),rs2(eqnengli),rs2(eqweizhi),rs2(eqxianzhuang),rs2(eqno),rs2(eqbumen),rs2(eqchejian)) '把数据集中的数据填写到相应的单元中
num=num+1
rs2.MoveNext
loop
function getTemporaryFile(myFileSystem)
dim tempFile,dotPos
tempFile=myFileSystem.getTempName
dotPos=instr(1,tempFile,.)
getTemporaryFile=mid(tempFile,1,dotPos)&xlsend function
set myFs=createObject(scripting.FileSystemObject)
filePos=server.mappath(\test)&\temp '要存放打印临时文件的临时目录
fileName=getTemporaryFile(myFs) '取得一个临时文件名
'myFs.DeleteFile filePos&*.xls '删除该目录下所有原先产生的临时打印文件
set myFs=nothing
objExcel.ActiveWorkbook.saveas filePos&filename
objExcel.quit
set objExcel=Nothing。
<!--#include file="conn.asp"--><HTML>
<HEAD>
<meta content="text/htmlcharset=gb2312" http-equiv="Content-Type">
<TITLE>生成EXCEL文件</TITLE>
</HEAD>
<body>
<hr size=1 align=left width=300px>
<%
dim rs,sql,filename,fs,myfile,objSpreadsheet,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("online.xls")
'--如果原来的EXCEL文件存在的话就删除
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'Set objSpreadsheet = Server.CreateObject("OWC.Spreadsheet")
'objSpreadsheet.Cells(iRow, iCol).Font.Bold = True
'objSpreadsheet.Cells(iRow, iCol).Font.Italic = False
'objSpreadsheet.Cells(iRow, iCol).Font.Size = 10
'objSpreadsheet.Cells(iRow, iCol).Halignment = 2 '居中
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)
Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据读出来
sql = "select * from 表名 order by id desc"
rs.Open sql,conn,1,1
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
response.Write("<a href='online.xls'>下载</a>")
end if
rs.Close
set rs = nothing
%>
asp导出到excel在开头加上这一句
Response.ContentType = "application/vnd.ms-excel"
如果导出结果为空白的,那就去掉它
一下是导出Excel代码
--------------------------------------------------------------------------------
<%
set rs=server.createobject("adodb.recordset")
sql="select * from provinceinfo where 1=1"
rs.open sql,objconn,1,1
Set ExcelApp =CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
Set ExcelBook = ExcelApp.Workbooks.Add
ExcelBook.WorkSheets(1).cells(1,1).value ="用户表"
ExcelBook.WorkSheets(1).cells(2,1).value = "用户编号"
ExcelBook.WorkSheets(1).cells(2,2).value = "登陆名"
ExcelBook.WorkSheets(1).cells(2,3).value = "真实姓名"
ExcelBook.WorkSheets(1).cells(2,4).value = "密码"
cnt =3
do while not rs.eof
ExcelBook.WorkSheets(1).cells(cnt,1).value = rs("provinceid")
ExcelBook.WorkSheets(1).cells(cnt,2).value = rs("province")
ExcelBook.WorkSheets(1).cells(cnt,3).value = rs("flag")
ExcelBook.WorkSheets(1).cells(cnt,4).value = rs("id")
rs.movenext
cnt = cint(cnt) + 1
loop
Excelbook.SaveAs "d:\yourfile.xls" '这个是数据导出完毕以后在D盘存成文件
ExcelApp.Application.Quit '导出以后退出Excel
Set ExcelApp = Nothing '注销Excel对象
%>
因你数据量大 另提示EXCEL相关信息
Excel 07-2003一个工作表最多可有65536行,行用数字1—65536表示最多可有256列,列用英文字母A—Z,AA—AZ,BA—BZ,……,IA—IV表示;一个工作簿中最多含有255个工作表,默认情况下是三个工作表;Excel 2007及以后版本,一个工作表最多可有1048576行,16384列;
所以建议用2007以上版本的excel
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)