ASP控制Excel的Xls属性
最近在帮学校做一个ASP系统,在今晚解决了一系列问题后,遇到了一个颇为棘手的问题——ASP控制Excel的Xls属性问题。为解决该问题,我尝试了以下两种方法:
一、方法介绍
(一)第一种方法
- 原理:这种方法通用性较强,适用于大多数主机。它通过ASP的一个组件直接将查询到的数据写入文件,让Excel自行识别文件格式为XLS 。
代码示例:
'定义文件路径 dim path path=server.MapPath("report.xls") '创建文件系统对象 set fso=server.CreateObject("Scripting.filesystemobject") '检查文件是否存在,若存在则删除 if fso.FileExists(path) then fso.deletefile(path) end if '创建文本文件 set myfile=fso.createtextfile(path,true) '创建记录集对象 set rs=server.CreateObject("adodb.recordset") '打开记录集,str为查询语句,conn为数据库连接对象 rs.open str,conn,3,4 '判断记录集是否为空 if rs.EOF and rs.BOF then Response.Write "暂时没有数据!" 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 '关闭记录集并释放对象 rs.Close set rs = nothing set myfile = nothing Set fs=Nothing
- 优点:通用性强,能在多数主机环境下使用。
- 缺点:在处理包含长数字(如学号、考号等)的数据时,Excel会将长数字识别为数字类型,并用科学计数法表示,这在某些场景下并不符合需求。由于这种生成xls文件方式的特殊性,基本难以找到解决该问题的方法。
(二)第二种方法
- 原理:此方法对Excel的操作更方便、更直接,但对主机环境要求较为苛刻。它通过创建Excel.Application对象,直接在内存中操作Excel文件,设置单元格格式、写入数据等。
- 代码示例:
'打开记录集,str为查询语句,conn为数据库连接对象
rs.open str,conn,3,4
'On error resume next
'判断记录集是否为空
if rs.eof then
Response.write "没有记录可导出! "
rs.close
set rs=nothing
response.End()
end if
'创建Excel应用程序对象
Dim App,Book,Shts,Sht
set App = CreateObject("Excel.Application")
'设置Excel不可见,不显示警告和界面
App.Visible = False
App.DisplayAlerts =true
App.Application.Visible = false
'添加Excel工作簿
App.WorkBooks.add
set Book = App.ActiveWorkBook
set Shts = Book.Worksheets
set Sht = Book.Sheets(1)
kk=0
'根据表单复选框的值设置表头
for j=1 to recordcounts
if request.Form("checkbox"&j)<>"" then
Sht.Range(chr(asc("A")+int(kk))&"1:"&chr(asc("A")+int(kk))&"1").Value =request.Form("checkbox"&j)
Sht.Columns("A:A").NumberFormatLocal = "0_ "
kk=kk+1
end if
next
'定义行数,从第二行开始写数据
Dim r
r=2
Dim DeferOctEmp,IsCheck
'遍历记录集,写入数据
do while not rs.eof
kk=0
for j=1 to recordcounts
kindname=request.Form("checkbox"&j)
'根据复选框的值确定字段名
if request.Form("checkbox"&i)="考生号" then kindname="ksh"
if request.Form("checkbox"&i)="录取号" then kindname="lqh"
if request.Form("checkbox"&i)="专业" then kindname="录取大专业"
if request.Form("checkbox"&i)="接收单位隶属部门" then kindname="单位隶"
if request.Form("checkbox"&i)="协议书编号" then kindname="xysh"
if kindname<>"" then
if not isnull(rs(""&kindname&"")) or trim(rs(""&kindname&""))<>"" then Sht.Range(chr(asc("A")+int(kk))&r&":"&chr(asc("A")+int(kk))&r).Value=rs(""&kindname&"")
kk=kk+1
end if
next
rs.movenext
r=r+1
loop
'关闭记录集并释放对象
rs.close
set rs=nothing
'设置自动列宽(代码中被注释掉,可根据需求启用)
'Sht.Range("A1:"&chr(asc("A")+int(recordcounts-1))&(r-1)).Columns.AutoFit
'定义Excel文件路径
Dim ExcelFile
ExcelFile="Excel/ecjtu"&session("user")&".xls"
'创建文件系统对象
Set fso = Server.CreateObject("Scripting.FileSystemObject")
'检查文件是否存在,若存在则删除
if fso.fileexists(Server.MapPath(ExcelFile)) then fso.deleteFile(Server.MapPath(ExcelFile))
set fso=nothing
'保存Excel文件
Book.SaveAs Server.MapPath(ExcelFile)
'检查保存过程中是否出错
if err.Number<>0 then
Response.write err.Description
App.Quit
set App = Nothing
response.end
end if
Book.Save
App.Quit
set App = Nothing
'重定向到生成的Excel文件
Response.Redirect ExcelFile
- 优点:对Excel操作精细,能更好地控制Excel文件的各种属性,如设置单元格格式等,在处理数据格式要求较高的场景中有明显优势。
- 缺点:对主机环境要求苛刻,需要服务器支持创建Excel.Application对象,部分服务器环境可能不允许或配置复杂。
二、问题分析与后续思考
针对第一种方法中长数字显示异常的问题,由于其通过简单写入文件让Excel自动识别,缺乏对数据格式的有效控制,目前难以找到直接解决方案。而第二种方法虽然能很好地控制数据格式,但主机环境限制是个大问题。在后续工作中,或许需要进一步探索在满足主机环境要求的前提下,优先使用第二种方法;若无法满足,再考虑对第一种方法进行改进,比如尝试在写入数据前对长数字进行特殊处理,或者寻找能在文件写入后强制Excel正确识别数据格式的方法。
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »