乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 130|回复: 3

CopyFromRecordset在AutoCADVBA中的应用

[复制链接]

120

主题

326

帖子

7

银币

中流砥柱

Rank: 25

铜币
806
发表于 2007-12-14 11:32:00 | 显示全部楼层 |阅读模式
CopyFromRecordset命令在ExcelVBA比较常用,将其移植到VBA与EXCLE通讯中,工作效率比较高。
以下程序摘自原意虽是VB程序EXCEL的数据交换,但应用于AutoCADVBA中效果也是比较好的。
  1. Private Sub CClick()
  2.     Dim cnt As New ADODB.Connection
  3.     Dim rst As New ADODB.Recordset
  4.    
  5.     Dim xlApp As Object
  6.     Dim xlWb As Object
  7.     Dim xlWs As Object
  8.    
  9.     Dim recArray As Variant
  10.    
  11.     Dim strDB As String
  12.     Dim fldCount As Integer
  13.     Dim recCount As Long
  14.     Dim iCol As Integer
  15.     Dim iRow As Integer
  16.    
  17.     ' Set the string to the path of your Northwind database
  18.     strDB = "c:\program files\Microsoft office\office11\samples\Northwind.mdb"
  19.   
  20.     ' Open connection to the database
  21.     cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  22.         "Data Source=" & strDB & ";"
  23.         
  24.     ' Open recordset based on Orders table
  25.     rst.Open "Select * From 订单", cnt
  26.    
  27.     ' Create an instance of Excel and add a workbook
  28.     Set xlApp = CreateObject("Excel.Application")
  29.     Set xlWb = xlApp.Workbooks.Add
  30.     Set xlWs = xlWb.Worksheets("Sheet1")
  31.   
  32.     ' Display Excel and give user control of Excel's lifetime
  33.     xlApp.Visible = True
  34.     xlApp.UserControl = True
  35.    
  36.     ' Copy field names to the first row of the worksheet
  37.     fldCount = rst.Fields.Count
  38.     For iCol = 1 To fldCount
  39.         xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
  40.     Next
  41.         
  42.     ' Check version of Excel
  43.     If Val(Mid(xlApp.Version, 1, InStr(1, xlApp.Version, ".") - 1)) > 8 Then
  44.         'EXCEL 2000 or 2002: Use CopyFromRecordset
  45.          
  46.         ' Copy the recordset to the worksheet, starting in cell A2
  47.         xlWs.Cells(2, 1).CopyFromRecordset rst
  48.         'Note: CopyFromRecordset will fail if the recordset
  49.         'contains an OLE object field or array data such
  50.         'as hierarchical recordsets
  51.         
  52.     Else
  53.         'EXCEL 97 or earlier: Use GetRows then copy array to Excel
  54.    
  55.         ' Copy recordset to an array
  56.         recArray = rst.GetRows
  57.         'Note: GetRows returns a 0-based array where the first
  58.         'dimension contains fields and the second dimension
  59.         'contains records. We will transpose this array so that
  60.         'the first dimension contains records, allowing the
  61.         'data to appears properly when copied to Excel
  62.         
  63.         ' Determine number of records
  64.         recCount = UBound(recArray, 2) + 1 '+ 1 since 0-based array
  65.         
  66.         ' Check the array for contents that are not valid when
  67.         ' copying the array to an Excel worksheet
  68.         For iCol = 0 To fldCount - 1
  69.             For iRow = 0 To recCount - 1
  70.                 ' Take care of Date fields
  71.                 If IsDate(recArray(iCol, iRow)) Then
  72.                     recArray(iCol, iRow) = Format(recArray(iCol, iRow))
  73.                 ' Take care of OLE object fields or array fields
  74.                 ElseIf IsArray(recArray(iCol, iRow)) Then
  75.                     recArray(iCol, iRow) = "Array Field"
  76.                 End If
  77.             Next iRow 'next record
  78.         Next iCol 'next field
  79.             
  80.         ' Transpose and Copy the array to the worksheet,
  81.         ' starting in cell A2
  82.         xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = _
  83.             TransposeDim(recArray)
  84.     End If
  85.     ' Auto-fit the column widths and row heights
  86.     xlApp.Selection.CurrentRegion.Columns.AutoFit
  87.     xlApp.Selection.CurrentRegion.Rows.AutoFit
  88.     ' Close ADO objects
  89.     rst.Close
  90.     cnt.Close
  91.     Set rst = Nothing
  92.     Set cnt = Nothing
  93.    
  94.     ' Release Excel references
  95.     Set xlWs = Nothing
  96.     Set xlWb = Nothing
  97.     Set xlApp = Nothing
  98. End SubFunction TransposeDim(v As Variant) As Variant
  99. ' Custom Function to Transpose a 0-based array (v)
  100.    
  101.     Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
  102.     Dim tempArray As Variant
  103.    
  104.     Xupper = UBound(v, 2)
  105.     Yupper = UBound(v, 1)
  106.    
  107.     ReDim tempArray(Xupper, Yupper)
  108.     For X = 0 To Xupper
  109.         For Y = 0 To Yupper
  110.             tempArray(X, Y) = v(Y, X)
  111.         Next Y
  112.     Next X
  113.    
  114.     TransposeDim = tempArray
  115. End Function

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

120

主题

326

帖子

7

银币

中流砥柱

Rank: 25

铜币
806
发表于 2007-12-14 13:11:00 | 显示全部楼层
  1. Sub Ss()
  2.   
  3.   Dim xlSheet
  4.   Set xlSheet = xlApp.sheets(2)
  5.   
  6.   
  7.   Dim ColNum, RowNum, pp(0 To 2) As Double, RowColText
  8.   Dim Ent As AcadEntity, tt As AcadText
  9.   ColNum = Array(0, 10, 24, 44, 52, 61, 69, 77, 86, 94, 103, 111, 119, 128, 136, 145, 153, 161, 170, 178)
  10.   ReDim Preserve ColNum(UBound(ColNum))
  11.   
  12.   RowNum = Array(0, 5, 11, 16, 22, 27, 32, 38, 43) ', 45, 48, 55)
  13.   RowCount = UBound(RowNum)
  14.   ReDim Preserve RowNum(UBound(RowNum))
  15.   ReDim RowColText(UBound(RowNum) - 1, UBound(ColNum) - 1)
  16. For Each Ent In ThisDrawing.ModelSpace
  17.   Select Case Ent.ObjectName
  18.     Case "AcDbText"
  19.       Set tt = Ent
  20.       
  21.       For ii = 0 To UBound(ColNum) - 1
  22.         If tt.InsertionPoint(0) > ColNum(ii) And tt.InsertionPoint(0)  RowNum(jj) And tt.InsertionPoint(1) < RowNum(jj + 1) Then
  23.           'Debug.Print jj + 1, "-----", tt.InsertionPoint(1)
  24.           RowNumCount = jj
  25.           Exit For
  26.         End If
  27.       Next jj
  28.       RowColText(RowNumCount, ColNumCount) = tt.TextString
  29. '      xlSheet.Cells(RowNumCount + 1, ColNumCount + 1).Value = tt.TextString
  30.   End Select
  31.   
  32. Next Ent
  33. xlSheet.Range("A2").Resize(RowCount, 19).Value = RowColText
  34.     Columns("A:S").Select
  35.     Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
  36.         OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  37.         :=xlPinYin, DataOption1:=xlSortNormal
  38. Debug.Print
  39. End Sub
回复

使用道具 举报

6

主题

34

帖子

2

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
58
发表于 2012-7-25 07:26:00 | 显示全部楼层
占位,学习
回复

使用道具 举报

1

主题

24

帖子

16

银币

初来乍到

Rank: 1

铜币
20
发表于 2021-8-8 16:38:00 | 显示全部楼层
学习学习
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-4-19 06:43 , Processed in 0.496525 second(s), 61 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表