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

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

   Dim intFieldCount                As Integer                    ' 字段数 
   Dim strFields                    As String                     ' 所有字段名 
   Dim i                            As Integer 

   Dim exlApplication               As Object                     ' Excel 实例 
   Dim exlBook                      As Object                     ' Excel 工作区 
   Dim exlSheet                     As Object                     ' Excel 当前要操作的工作表 

   On Error GoTo LocalErr 

   Me.MousePointer = vbHourglass 

   '// 创建 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