明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2290|回复: 3

CopyFromRecordset在AutoCADVBA中的应用

  [复制链接]
发表于 2007-12-14 11:32:00 | 显示全部楼层 |阅读模式
CopyFromRecordset命令在ExcelVBA比较常用,将其移植到AutoCADVBA与EXCLE通讯中,工作效率比较高。
以下程序摘自http://support.microsoft.com/kb/246335/zh-cn原意虽是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 Sub
  99. Function TransposeDim(v As Variant) As Variant
  100. ' Custom Function to Transpose a 0-based array (v)
  101.    
  102.     Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
  103.     Dim tempArray As Variant
  104.    
  105.     Xupper = UBound(v, 2)
  106.     Yupper = UBound(v, 1)
  107.    
  108.     ReDim tempArray(Xupper, Yupper)
  109.     For X = 0 To Xupper
  110.         For Y = 0 To Yupper
  111.             tempArray(X, Y) = v(Y, X)
  112.         Next Y
  113.     Next X
  114.    
  115.     TransposeDim = tempArray
  116. End Function


 楼主| 发表于 2007-12-14 13:11:00 | 显示全部楼层
本帖最后由 作者 于 2007-12-14 14:49:55 编辑
  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) < ColNum(ii + 1) Then
  23.           'Debug.Print ii + 1, tt.InsertionPoint(0)
  24.           ColNumCount = ii
  25.           Exit For
  26.         End If
  27.       Next ii
  28.       
  29.       For jj = 0 To UBound(RowNum)
  30.         If tt.InsertionPoint(1) > RowNum(jj) And tt.InsertionPoint(1) < RowNum(jj + 1) Then
  31.           'Debug.Print jj + 1, "-----", tt.InsertionPoint(1)
  32.           RowNumCount = jj
  33.           Exit For
  34.         End If
  35.       Next jj
  36.       RowColText(RowNumCount, ColNumCount) = tt.TextString
  37. '      xlSheet.Cells(RowNumCount + 1, ColNumCount + 1).Value = tt.TextString
  38.   End Select
  39.   
  40. Next Ent
  41. xlSheet.Range("A2").Resize(RowCount, 19).Value = RowColText
  42.     Columns("A:S").Select
  43.     Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
  44.         OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
  45.         :=xlPinYin, DataOption1:=xlSortNormal
  46. Debug.Print
  47. End Sub
发表于 2012-7-25 07:26:47 | 显示全部楼层
占位,学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 04:36 , Processed in 0.154495 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表