用vba实现将记录集输出到Excel模板

2019-11-04 08:28:02于海丽

         '// 增加一个命名范围,作用是在导入时所需的范围 
         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 

   '// 通常字段数不会太多,所以到 26*3 目前已经够了。 
   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, & _