<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-348  評論-598  文章-0  trackbacks-0
    因為項目需要,所以從網上找了一個類,但是那個類問題比較多,所以自己修改了一下,增加容錯程度,提升一些性能,里面有部分代碼是根據我的項目修改的,所以大家在使用的時候自己改一下就可以了。

    使用方法:
    <%On Error Resume Next%>
    <!--#include file="../Include/Constants.Class.asp"-->
    <!--#include file="../Include/Config.Class.asp"-->
    <!--#include file="../Include/DBControl.Class.asp"-->
    <!--#include file="../Include/FunctionLib.Class.asp"-->
    <!--#include file="../Include/Manager.Class.asp"-->
    <!--#include file="../Include/Export2Excel.Class.asp"-->
    <%
    Dim Cfg,Db,Flib,Admin,Con,newExcel,url
    Set Cfg=New Config
    Set Con=New Constants
    Set Admin=New Manager
    Set Flib=New FunctionLib
    Set Db=New DBControl

    If session(request.QueryString("sql"))="" or session(request.QueryString("field"))="" Then
        Flib.MessageBox 
    "Excel導出頁面參數出錯!請聯系管理員","",0
    End If

    response.Write 
    "導出過程可能需要很長時間,請稍等<br>"
    response.Flush()
    set newExcel = New Export2Excel
    newExcel.FilePath 
    = "Excel/"
    newExcel.Sql 
    = session(request.QueryString("sql"))
    newExcel.Field 
    = session(request.QueryString("field"))
    response.write newExcel.export2Excel()


    %
    >

    類的源代碼:
    <%
    '  使用方法:
    '
      set newExcel = New Export2Excel
    '
      newExcel.FilePath = "/mail/excel/"----------------------------------路徑
    '
      newExcel.Sql = "select * from user"-------------------------------查詢語句
    '
      newExcel.Field = "帳號||姓名||所屬部門||"----------------------輸出列名
    '
      response.write newExcel.export2Excel()
    '
    類開始
    Class Export2Excel
    '聲明常量、變量
        Private strFilePath,strTitle,strSql,strField,strRows,strCols
        
    Private strCn,strHtml,strPath,strServerPath,Filename
        
    Private objDbCn,objRs
        
    Private objXlsApp,objXlsWorkBook,objXlsWorkSheet
        
    Private arrField
        
    '初始化類
        Private Sub Class_Initialize()
         
    set objDbCn = Db
         strTitle 
    = "查詢結果"
         strFilePath
    ="Excel/"
         strRows 
    = 2
         strCols 
    = 1
        
    End Sub
        
    '銷毀類
        Private Sub Class_Terminate()
        
    End Sub
        
    '屬性FilePath
        Public Property Let FilePath(value)
         strFilePath 
    = value
         strServerPath
    =strFilePath
        
    End Property
        
    Public Property Get FilePath()
         FilePath 
    = strFilePat
        
    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()
         
    if strSql = "" or strField = "" then
          response.write 
    "參數設置錯誤,請與管理員聯系!謝謝"
          response.end
         
    end if
         
         strFilePath 
    = GetFilePath(Server.mappath(strFilePath&"upload.asp"),"\")
         
    set objFso = createobject("scripting.filesystemobject")
         
    if objFso.FolderExists(strFilePath) = False then
          objFso.Createfolder(strFilePath)
         
    end if
         Filename
    =cstr(createFileName()) & ".xls"
         strFileName 
    = strFilePath & Filename 
         objDbCn.Open()
         
    set objRs = objDbCn.execute(strSql)
         
    if objRs.EOF And objRs.BOF then
          strHtml 
    = "抱歉,暫時沒有任何合適的數據導出,如有疑問,請與管理員聯系!"
         
    else
          
    set objXlsApp = server.CreateObject("Excel.Application")
          objXlsApp.Visible 
    = false
          objXlsApp.WorkBooks.Add
          
    set objXlsWorkBook = objXlsApp.ActiveWorkBook
          
    set objXlsWorkSheet = objXlsWorkBook.WorkSheets(1)
          arrField 
    = split(strField,"||")
          
          
    for f = 0 to Ubound(arrField)
           objXlsWorkSheet.Cells(
    1,f+1).Value = arrField(f)
           
    'response.Write arrField(f)&" "
          next
          
    'response.Write "<br>"
          objRs=objRs.getRows()
          
    If instr(Sql,"exportEnterprise ")=0 then
              
    for c=0 to ubound(objRs,2)
                  
    If response.IsClientConnected=false then exit for '數據多導出時間很長,所以需要探測下客戶端是否還在連接
                  response.Write "正在導出第"&cstr(c+1)&"條<br>"
                response.Flush()
               
    for f = 0 to ubound(objRs,1)
                       
    If response.IsClientConnected=false then exit for
                 objXlsWorkSheet.Cells(c
    +2,f+1).Value = trim(Cstr(objRs(f,c)))&VBCR
                 
    'objXlsWorkSheet.Columns(f+1).ColumnWidth=Len(Cstr(objRs(f,c)))*2
               next
              
    next
              
          
    Else
                
    for c=0 to ubound(objRs,2)
                  
    If response.IsClientConnected=false then exit for
                  response.Write 
    "正在導出第"&cstr(c+1)&"條<br>"
                response.Flush()
               
    for f = 0 to ubound(objRs,1)
                   
    If response.IsClientConnected=false then exit for
                
    If f<>1 then
                 objXlsWorkSheet.Cells(c
    +2,f+1).Value = trim(Cstr(objRs(f,c)))&VBCR
                 
    'objXlsWorkSheet.Columns(f+1).ColumnWidth=Len(Cstr(objRs(f,c)))*2
                Else
                 objXlsWorkSheet.Cells(c
    +2,f+1).Value = trim(replace(replace(Cstr(objRs(f,c)),"0",""),"|"," "))&VBCR
                 
    'objXlsWorkSheet.Columns(f+1).ColumnWidth=Len(Cstr(objXlsWorkSheet.Cells(c+2,f+1).Value))*2            
                End If
               
    next
              
    next
          
    End If
          
          
    '必不可少,否則會出現錯誤
          If objFso.fileExists(strFileName)=true then
              objFso.deletefile strFileName
          
    End if
            response.Write 
    "導出成功!<br>"
            response.Flush()      
      
          objXlsWorkSheet.SaveAs strFileName
          
          strHtml 
    = "<script>location.href='" & GetFilePath(Request.ServerVariables("HTTP_REFERER"),"/")&strServerpath&Filename  & "';</script>"
          objXlsApp.Quit
    '重要
          set objXlsWorkSheet = nothing
          
    set objXlsWorkBook = nothing
          
    set objXlsApp = nothing
         
    end if
         objDbCn.Close()
         
    set objRs = nothing
         
    if err > 0 then
          strHtml 
    = "系統忙,請稍后重試"
         
    end if
         export2Excel 
    = strHtml
        
    End Function
        
    '函數
        Public Function createFileName()
         
    If Admin.id<>"" then
              fName
    =Admin.id
         
    Else
             fName
    =now
             fName
    =replace(fName,":","")
             fName
    =replace(fName,"-","")
             fName
    =replace(fName," ","")
         
    End If
         createFileName
    =fName
        
    End Function
            
        
    Public function GetFilePath(FullPath,str)
          
    If FullPath <> "" Then
            GetFilePath 
    = left(FullPath,InStrRev(FullPath, str))
            
    Else
            GetFilePath 
    = ""
          
    End If
        
    End function     
        
    'Public Function debug(varStr)
        ' response.write varStr
        ' response.end
        'End Function
        '類結束
    End Class
    %
    >



    ---------------------------------------------------------
    專注移動開發

    Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
    posted on 2007-07-29 16:28 TiGERTiAN 閱讀(2212) 評論(8)  編輯  收藏 所屬分類: VB/ASP

    評論:
    # re: asp導出excel用到的類[未登錄] 2008-09-17 10:41 | spring
    能發個源碼給我嗎,現在用的這些,<!--#include file="../Include/Constants.Class.asp"-->
    <!--#include file="../Include/Config.Class.asp"-->
    <!--#include file="../Include/DBControl.Class.asp"-->
    <!--#include file="../Include/FunctionLib.Class.asp"-->
    <!--#include file="../Include/Manager.Class.asp"-->沒有.E-MAIL:djj128@163.com  回復  更多評論
      
    # re: asp導出excel用到的類 2008-09-17 12:47 | TiGERTiAN
    @spring
    這些沒什么用的,類的代碼是全的。調用代碼就是:
    set newExcel = New Export2Excel
    newExcel.FilePath = "Excel/"
    newExcel.Sql = session(request.QueryString("sql"))
    newExcel.Field = session(request.QueryString("field"))
    response.write newExcel.export2Excel()
    其他沒有了。  回復  更多評論
      
    # re: asp導出excel用到的類 2008-10-21 23:34 | 不太冷
    If instr(Sql,"exportEnterprise ")=0 then
    exportEnterprise和Sql是什么地方的?我看不明白,把您的代碼運行了下,不生成任何文件  回復  更多評論
      
    # re: asp導出excel用到的類 2008-10-21 23:56 | TiGERTiAN
    @不太冷
    這個條件語句是根據我自己的程序需要來的,可以把這個條件限制去掉,稍微修改下就好了  回復  更多評論
      
    # re: asp導出excel用到的類 2008-10-22 00:20 | 不太冷
    @TiGERTiAN
    不知道是否冒犯,我還在調試這個程序,可惜一直未能成功,你能加我QQ嗎?
    問幾個問題,1741821
      回復  更多評論
      
    # re: asp導出excel用到的類 2009-02-11 14:45 | lzq
    太行了.  回復  更多評論
      
    # re: asp導出excel用到的類 2009-02-27 15:08 | asdfdg
    使用說明太不明確了,好多地方都要改的
    哪里連數據庫都不明確  回復  更多評論
      
    # re: asp導出excel用到的類 2009-06-11 14:27 | 站長
    很好用的站長查詢網站 http://www.ngiv.cn
    很全的技術論文 http://bbs.ngiv.cn  回復  更多評論
      
    主站蜘蛛池模板: 亚洲精品无码Av人在线观看国产| 亚洲小视频在线观看| 99久久99这里只有免费的精品| 亚洲色偷偷偷鲁综合| 亚洲免费电影网站| 色窝窝亚洲AV网在线观看| 狠狠色伊人亚洲综合成人| 黄色成人网站免费无码av| 免费人人潮人人爽一区二区| 亚洲国产精品福利片在线观看| 91香蕉视频免费| 成人免费乱码大片A毛片| 亚洲第一页中文字幕| mm1313亚洲国产精品美女| 久久99国产综合精品免费| 色婷婷精品免费视频| 亚洲在成人网在线看| 国产亚洲美女精品久久久| 免费一本色道久久一区| 中文无码成人免费视频在线观看| 亚洲日本va在线观看| 亚洲国产另类久久久精品小说| 韩国二级毛片免费播放| 中文字幕在线免费| 中文无码日韩欧免费视频| 亚洲欧洲无码一区二区三区| 亚洲va在线va天堂va不卡下载| 亚洲Av无码乱码在线播放| 成人性生交大片免费看无遮挡 | 国产亚洲视频在线观看| 亚洲成aⅴ人片在线观| 亚洲愉拍99热成人精品热久久| 精品免费国产一区二区三区| 亚洲黄色免费网址| 国产婷婷成人久久Av免费高清| 青青青视频免费观看| 亚洲爆乳无码专区www| 成人区精品一区二区不卡亚洲| 久久亚洲私人国产精品vA| 亚洲啪啪综合AV一区| 亚洲第一黄片大全|