ASP 导出Excel

jaacc_excel.asp

<!--#include file=common.asp-->
<%
'**********************************************
' code by asp导出excel通用
' 修改引用 by 子言(jastudio)
' qq:23638564 email:kpggdf@163.com
' web:www.gdsspt.com
'**********************************************
dim xibua
dim mysql
xibua = request.querystring("ids")
if xibua="all" then
mysql = "select * from singup"
else
mysql = "select * from singup where [系部]='"&xibua&"'"
end if
server.scripttimeout=100000 '处理时间较长,设置值应大一点
on error resume next
set objexcelapp = createobject("excel.application")
objexcelapp.displayalerts = false
objexcelapp.application.visible = false
objexcelapp.workbooks.add
set objexcelbook = objexcelapp.activeworkbook
set objexcelsheets = objexcelbook.worksheets
set objspreadsheet = objexcelbook.sheets(1)
dim objrs
set objrs = server.createobject("adodb.recordset")
objrs.open mysql,conn,1,3
if objrs.eof then
response.write("error")
respose.end
end if

dim objfield, icol, irow
icol = 1 '取得列号
irow = 1 '取得行号
objspreadsheet.cells(irow, icol).value = ""&xibua&"部的报名情况" '单元格插入数据
objspreadsheet.columns(icol).shrinktofit=true '设定是否自动适应表格单元大小(单元格宽不变)
'设置excel表里的字体
objspreadsheet.cells(irow, icol).font.bold = true '单元格字体加粗
objspreadsheet.cells(irow, icol).font.italic = false '单元格字体倾斜
objspreadsheet.cells(irow, icol).font.size = 20 '设置单元格字号
objspreadsheet.cells(irow, icol).paragraphformat.alignment=1 '设置单元格对齐格式:居中
objspreadsheet.cells(irow,icol).font.name="宋体" '设置单元格字体
objspreadsheet.cells(irow,icol).font.colorindex=2 '设置单元格文字的颜色,颜色可以查询,2为白色
objspreadsheet.range("a1:f1").merge '合并单元格(单元区域)
objspreadsheet.range("a1:f1").interior.colorindex = 1 '设计单元络背景色
'objspreadsheet.range("a2:f2").wraptext=true '设置字符回卷(自动换行)
irow=irow+1
for each objfield in objrs.fields
'objspreadsheet.columns(icol).shrinktofit=true
objspreadsheet.cells(irow, icol).value = objfield.name
'设置excel表里的字体
objspreadsheet.cells(irow, icol).font.bold = true
objspreadsheet.cells(irow, icol).font.italic = false
objspreadsheet.cells(irow, icol).font.size = 20
objspreadsheet.cells(irow, icol).halignment = 2 '居中
icol = icol + 1
next 'objfield
'display all of the data
do while not objrs.eof
irow = irow + 1
icol = 1
for each objfield in objrs.fields
if isnull(objfield.value) then
objspreadsheet.cells(irow, icol).value = ""
else
objspreadsheet.columns(icol).shrinktofit=true
objspreadsheet.cells(irow, icol).value = objfield.value
objspreadsheet.cells(irow, icol).halignment = 2
objspreadsheet.cells(irow, icol).font.bold = false
objspreadsheet.cells(irow, icol).font.italic = false
objspreadsheet.cells(irow, icol).font.size = 10
'objspreadsheet.cells(irow, icol).halignment = 2
objspreadsheet.cells(irow, icol).paragraphformat.alignment=1
end if
icol = icol + 1
next 'objfield
objrs.movenext
loop

dim savename
savename=xibua
dim objexcel
dim excelpath
excelpath = "" & savename & ".xls"
objexcelbook.saveas server.mappath(excelpath)
response.write "<center><b>导出成功,请选择继续操作</b></center>"
response.write "<table width=90% bgcolor=gray bgcolor=cccccc cellspacing=1 cellpadding=3 align=center>"
response.write "<tr align=center bgcolor=#6699cc style=color:white> <td>"
response.write("<font color=green>√</font><a href='" & excelpath & "'>下载 </a>") & "  <font color=green>√</font><a href=javascript:history.back()>返回上一页</a>"
response.write "</td></tr></table>"
objexcelapp.quit
set objexcelapp = nothing
%>

posted on 2014-10-28 15:25  山冈龙  阅读(234)  评论(0编辑  收藏  举报

导航