欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用vba實現(xiàn)將記錄集輸出到Excel模板

 更新時間:2007年02月28日 00:00:00   作者:  

復制代碼 代碼如下:

'************************************************ 
'** 函數(shù)名稱:  ExportTempletToExcel 
'** 函數(shù)功能:  將記錄集輸出到 Excel 模板 
'** 參數(shù)說明: 
'**            strExcelFile         要保存的 Excel 文件 
'**            strSQL               查詢語句,就是要導出哪些內(nèi)容 
'**            strSheetName         工作表名稱 
'**            adoConn              已經(jīng)打開的數(shù)據(jù)庫連接 
'** 函數(shù)返回: 
'**            Boolean 類型 
'**            True                 成功導出模板 
'**            False                失敗 
'** 參考實例: 
'**            Call ExportTempletToExcel(c:\\text.xls,查詢語句,工作表1,adoConn) 
'************************************************ 
Private Function ExportTempletToExcel(ByVal strExcelFile As String, _ 
                                      ByVal strSQL As String, _ 
                                      ByVal strSheetName As String, _ 
                                      ByVal adoConn As Object) As Boolean 
   Dim adoRt                        As Object 
   Dim lngRecordCount               As Long                       ' 記錄數(shù) 
   Dim intFieldCount                As Integer                    ' 字段數(shù) 
   Dim strFields                    As String                     ' 所有字段名 
   Dim i                            As Integer 

   Dim exlApplication               As Object                     ' Excel 實例 
   Dim exlBook                      As Object                     ' Excel 工作區(qū) 
   Dim exlSheet                     As Object                     ' Excel 當前要操作的工作表 

   On Error GoTo LocalErr 

   Me.MousePointer = vbHourglass 

   '// 創(chuàng)建 ADO 記錄集對象 
   Set adoRt = CreateObject(ADODB.Recordset) 

   With adoRt 
      .ActiveConnection = adoConn 
      .CursorLocation = 3           'adUseClient 
      .CursorType = 3               'adOpenStatic 
      .LockType = 1                 'adLockReadOnly 
      .Source = strSQL 
      .Open 

      If .EOF And .BOF Then 
         ExportTempletToExcel = False 
      Else 
         '// 取得記錄總數(shù),+ 1 是表示還有一行字段名名稱信息 
         lngRecordCount = .RecordCount + 1 
         intFieldCount = .Fields.Count - 1 

         For i = 0 To intFieldCount 
            '// 生成字段名信息(vbTab 在 Excel 里表示每個單元格之間的間隔) 
            strFields = strFields & .Fields(i).Name & vbTab 
         Next 

         '// 去掉最后一個 vbTab 制表符 
         strFields = Left$(strFields, Len(strFields) - Len(vbTab)) 

         '// 創(chuàng)建Excel實例 
         Set exlApplication = CreateObject(Excel.Application) 
         '// 增加一個工作區(qū) 
         Set exlBook = exlApplication.Workbooks.Add 
         '// 設(shè)置當前工作區(qū)為第一個工作表(默認會有3個) 
         Set exlSheet = exlBook.Worksheets(1) 
         '// 將第一個工作表改成指定的名稱 
         exlSheet.Name = strSheetName 

         '// 清除“剪切板” 
         Clipboard.Clear 
         '// 將字段名稱復制到“剪切板” 
         Clipboard.SetText strFields 
         '// 選中A1單元格 
         exlSheet.Range(A1).Select 
         '// 粘貼字段名稱 
         exlSheet.Paste 

         '// 從A2開始復制記錄集 
         exlSheet.Range(A2).CopyFromRecordset adoRt 
         '// 增加一個命名范圍,作用是在導入時所需的范圍 
         exlApplication.Names.Add strSheetName, = & strSheetName & !$A$1:$ & _ 
                                  uGetColName(intFieldCount + 1) & $ & lngRecordCount 
         '// 保存 Excel 文件 
         exlBook.SaveAs strExcelFile 
         '// 退出 Excel 實例 
         exlApplication.Quit 

         ExportTempletToExcel = True 
      End If 
      'adStateOpen = 1 
      If .State = 1 Then 
         .Close 
      End If 
   End With 

LocalErr: 
   '********************************************* 
   '** 釋放所有對象 
   '********************************************* 
   Set exlSheet = Nothing 
   Set exlBook = Nothing 
   Set exlApplication = Nothing 
   Set adoRt = Nothing 
   '********************************************* 

   If Err.Number <> 0 Then 
      Err.Clear 
   End If 

   Me.MousePointer = vbDefault 
End Function 

'// 取得列名 
Private Function uGetColName(ByVal intNum As Integer) As String 
   Dim strColNames                  As String 
   Dim strReturn                    As String 

   '// 通常字段數(shù)不會太多,所以到 26*3 目前已經(jīng)夠了。 
   strColNames = A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z, & _ 
                 AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ, & _ 
                 BA,BB,BC,BD,BE,BF,BG,BH,BI,BJ,BK,BL,BM,BN,BO,BP,BQ,BR,BS,BT,BU,BV,BW,BX,BY,BZ 
   strReturn = Split(strColNames, ,)(intNum - 1) 
   uGetColName = strReturn 
End Function 

相關(guān)文章

最新評論