网站首页 手机版
 注册 登录
您现在的位置: 畅无忧设计 >> 网络编程 >> ASP教程 >> 正文
最新文章
· FSO 组件asp生成html静态页面碰到缓存
· asp实现长文章自动分页插件
· 在ASP中访问和更新Cookies集合
· ASP错误提示大全
· 学习ASP的几个观点
· ASP用两级联动下拉列表来显示大类和小
· ASP取当前页面地址和参数
· ASP删除记录的同时删除相关图片
· asp将查询结果导出到excel
· ASP批量导入Excel到Access或者Sql Se
热门文章
 化境ASP无组件上传类 - upload_5xs
 一个获取ACCESS数据库表名以及表名
 asp将查询结果导出到excel
 艾恩ASP无组件上传修改版
 ASP批量导入Excel到Access或者Sql 
 ASP读取数据库的Flash+JS图片切换特
 ASP用两级联动下拉列表来显示大类和
 ASP+JS实现网页歌曲连播、点播功能
 使用ASP重启服务器
 asp批量替换access数据库中指定字段
相关文章
没有相关文章
ASP实现Access数据库在线转换为Execl的类
来源:中国asp之家 更新时间:2009/5/14 17:23:17 阅读次数:
字体:[ ] 我要投稿

由于下载这个clsExport2Excel 代码时,已经找不到代码出处

感谢上传这个类的朋友,为大家带来了方便

我只对其中的属性传递做了一些调整,希望更能贴近实际应用。

<%
'lucker.chu 留
'
'类开始
Class clsExport2Excel
'声明常量、变量
Private strFilePath,strTitle,strSql,strField,strRows,strCols,cFileName,DataBasePath
Private strCn,strHtml,strPath
Private objDbCn,objRs
Private objXlsApp,objXlsWorkBook,objXlsWorkSheet
Private arrField

'初始化类
Private Sub Class_Initialize()
strFilePath = ".\"
strTitle = "查询结果"
strRows = 2
strCols = 1
End Sub

'销毁类
Private Sub Class_Terminate()

End Sub

'数据表位置
Public Property Let DataPath(value)
DataBasePath = value
End Property

Public Property Get DataPath()
DataPath = DataBasePath
End Property

'属性FilePath
Public Property Let FilePath(value)
strFilePath = value
End Property

Public Property Get FilePath()
FilePath = strFilePath
End Property

'文件名
Public Property Let FileName(value)
cFileName = value
End Property

Public Property Get FileName()
FileName = cFileName
End Property

'属性Title
Public Property Let Title(value)
strTitle = value
End Property

Public Property Get Title()
Title = strTitle
End Property

'属性Sql
Public Property Let Sql(value)
strSql = value
End Property

Public Property Get Sql()
Sql = strSql
End Property

'属性Field
Public Property Let Field(value)
strField = value
End Property

Public Property Get Field()
Field = strField
End Property

'属性Rows
Public Property Let Rows(value)
strRows = value
End Property

Public Property Get Rows()
Rows = strRows
End Property

'属性Cols
Public Property Let Cols(value)
strCols = value
End Property

Public Property Get Cols()
Cols = strCols
End Property


Public Function export2Excel()
DataBasePath=Server.MapPath(DataBasePath) '修改要打开的数据表所在位置
strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataBasePath
set objDbCn = server.CreateObject("adodb.connection")
objDbCn.open strCn

if strSql = "" or strField = "" then
   response.write "参数设置错误,请与管理员联系!谢谢"
   response.end
end if

if right(strFilePath,1) = "/" or right(strFilePath,1) = "\" then
   strFilePath = left(strFilePath,len(strFilePath)-1)
end if
if instr("/",strFilePath) > 0 then
   strFilePath = replace(strFilePath,"/","\")
end if
strFilePath = strFilePath & "\"

set objFso = createobject("scripting.filesystemobject")
if objFso.FolderExists(server.mappath(strFilePath)) = False then
   objFso.Createfolder(server.mappath(strFilePath))
end if

strFileName = strFilePath & cFileName

set objRs = server.CreateObject("adodb.RecordSet")
objRs.open strSql,objDbCn,3,3
if objRs.recordcount <= 0 then
   strHtml = "暂时没有任何合适的数据导出,如有疑问,请与管理员联系!抱歉"
else
   set objXlsApp = server.CreateObject("Excel.Application")
   objXlsApp.Visible = false
   objXlsApp.WorkBooks.Add

   set objXlsWorkBook = objXlsApp.ActiveWorkBook
   set objXlsWorkSheet = objXlsWorkBook.WorkSheets(1)

   objXlsWorkSheet.Cells(1,1).Value = strTitle

   arrField = split(strField,"||")
   for f = 0 to Ubound(arrField)
    objXlsWorkSheet.Cells(2,f+1).Value = arrField(f)
   next

   for c = 1 to objRs.recordcount
    for f = 0 to objRs.fields.count - 1
     '''身份证号码特殊处理
     if objRs.fields(f).name = "pm_field_41325" or objRs.fields(f).name = "cardID" then
      objXlsWorkSheet.Cells(c+2,f+1).Value = "'" & objRs.fields(f).value
     '''就业特殊处理
     elseif objRs.fields(f).name = "JiuYe" then
      select case objRs.fields(f).value
       case 1
        objXlsWorkSheet.Cells(c+2,f+1).Value = "是"
       case 0
        objXlsWorkSheet.Cells(c+2,f+1).Value = "否"
       case -1
        objXlsWorkSheet.Cells(c+2,f+1).Value = "(未知)"
      end select
     else
      objXlsWorkSheet.Cells(c+2,f+1).Value = objRs.fields(f).value
     end if
    next
    objRs.movenext
   next

   objXlsWorkSheet.SaveAs server.mappath(strFileName)

   strHtml = "Excel文件已经导出成功,您可以<a href='" & strFileName & "' target='_blank'>打开</a>文件并将文件另存到本地目录中!"
   'strhtml = server.mappath(strFileName)
   objXlsApp.Quit
   set objXlsWorkSheet = nothing
   set objXlsWorkBook = nothing
   set objXlsApp = nothing
end if
objRs.close
set objRs = nothing

if err > 0 then
   strHtml = "Excel文件导出时出现意外错误,请<a href='#' onclick='window.history.back();'>返回</a>,如有疑问,请与管理员联系!抱歉"
end if

export2Excel = strHtml
End Function

'Public Function debug(varStr)
' response.write varStr
' response.end
'End Function

'类结束
End Class

%>
类的使用方法:

<%
Function createFileName()
fName=now
fName=replace(fName,":","")
fName=replace(fName,"-","")
fName=replace(fName," ","")
createFileName=fName
End Function

'on error resume next
'引用内容
set newExcel = New clsExport2Excel    '调用MdbToExecl输出类
newExcel.DataPath = "OADB/DB.MDB"    '数据源
newExcel.FileName = "okok.xls"     'Execl存放的文件名
newExcel.FilePath = "/execl"     'Execl文件存放的位置
newExcel.Sql = "select * from Information" '查询语句
newExcel.Title = ""        '输出标题
newExcel.Field = "wenjm||tuplj||"    '输出列名
response.write newExcel.export2Excel()
%>

  • 上一篇文章:
  • 下一篇文章:
  • 关于我们 - 联系我们 - 广告服务 - 在线投稿 - 友情链接 - 网站地图 - 版权声明
    CopyRight 2008-2010, CWYDESIGN.COM - 畅无忧设计, Inc. All Rights Reserved
    滇ICP备09005765号