乐筑天下

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

AutoCad+vb+excel+CopyFromRecordset

[复制链接]

120

主题

326

帖子

7

银币

中流砥柱

Rank: 25

铜币
806
发表于 2008-3-27 22:01:00 | 显示全部楼层 |阅读模式
遍历图形文件的所有实体数据到excel,
用CopyFromRecordset效率要比用ExcelAndMdbData.xlSheet.Cells(ii, 1) = .Backward方法快数据倍.
遍历图形实体数据后,用CopyFromRecordset到excel用时为
遍历图形实体数据到数据集,用时:21:35:03-21:36:35
数据集:用CopyFromRecordset到excel用时为 21:36:35 -- 21:36:35
而用逐行逐列循环,将ExcelAndMdbData.xlSheet.Cells(ii, 1) = .Backward输到excel,用时约3分钟.
程序如下:
  1. [code]Option Explicit
  2. Dim boo As Boolean
  3. Dim obj_Acad As Object, obj_Doc As Object, obj_ModelSpace As Object
  4. Const RadianToDegree As Double = 180 / 3.1415926535897
  5. Const DegreeToRadian As Double = 3.1415926535897 / 180
  6. Const Pi = 3.1415926535897
  7. Dim BaseGraphic As New BaseGraphic
  8. Dim ExcelAndMdbData As New ExcelAndMdbData
  9. Public Function rr()
  10.   Debug.Print Time()
  11.   Dim Ent As Object
  12.   Dim ii As Integer, jj As Integer
  13.   Dim TitleVar As Variant
  14.   Dim adoRecordset     As ADODB.Recordset, rs As ADODB.Recordset
  15.   ''  TitleVar = Array("Backward", "Height", "InsertionPoint0", "InsertionPoint(1)", "InsertionPoint(2)", "Layer", "Linetype", "LinetypeScale", "Lineweight", "ObliqueAngle", "OwnerID", "PlotStyleName", "Rotation", "ScaleFactor", "StyleName", "TextAlignmentPoint(0)", "TextAlignmentPoint(1)", "TextAlignmentPoint(2)", "Alignment", "TextString", "Visible")
  16.   For jj = 0 To 20
  17.     ExcelAndMdbData.xlSheet.Cells(1, jj + 1) = TitleVar(jj)
  18.   Next jj
  19.   
  20.   
  21.   Dim pp As Variant, ppp As Variant
  22.   Dim ExcelData As Variant
  23.    '根据数组的大小初始化记录集
  24.    Set adoRecordset = New ADODB.Recordset
  25.    Set rs = New ADODB.Recordset
  26.   ''
  27.   
  28.   For jj = 0 To 20
  29.     'adoRecordset.Fields.Append TitleVar(jj), adVariant, , adFldMayBeNull
  30.     adoRecordset.Fields.Append TitleVar(jj), adBSTR
  31. 特别关注:
  32. , adVariant, , adFldMayBeNull---------出现如下错误:对象‘CopyFromRecordset’的方法‘Range’失败   
  33.    
  34. 改为, adBSTR以下程序通过
  35.   adoRecordset.Open
  36.   ''数组到数据集
  37.   
  38.   
  39.   ii = 0: jj = 1
  40.   For Each Ent In BaseGraphic.obj_ModelSpace
  41.     ReDim ExcelData(ii, 20)
  42.     With Ent
  43.    
  44.       Select Case .ObjectName
  45.         Case "AcDbText"
  46.           adoRecordset.AddNew   '加n条记录,即为   DataGrid   添加n空行
  47.           pp = .InsertionPoint
  48.           ppp = .TextAlignmentPoint
  49.           ii = ii
  50.           adoRecordset.Fields(0) = .Backward
  51.           adoRecordset.Fields(1) = .Height
  52.           adoRecordset.Fields(2) = pp(0)
  53.           adoRecordset.Fields(3) = pp(1)
  54.           adoRecordset.Fields(4) = pp(2)
  55.           adoRecordset.Fields(5) = .Layer
  56.           adoRecordset.Fields(6) = .Linetype
  57.           adoRecordset.Fields(8 - 1) = .LinetypeScale
  58.           adoRecordset.Fields(9 - 1) = .Lineweight
  59.           adoRecordset.Fields(10 - 1) = .ObliqueAngle
  60.           adoRecordset.Fields(11 - 1) = .OwnerID
  61.           adoRecordset.Fields(12 - 1) = .PlotStyleName
  62.           adoRecordset.Fields(13 - 1) = .Rotation
  63.           adoRecordset.Fields(14 - 1) = .ScaleFactor
  64.           adoRecordset.Fields(15 - 1) = .StyleName
  65.           adoRecordset.Fields(16 - 1) = ppp(0)
  66.           adoRecordset.Fields(17 - 1) = ppp(1)
  67.           adoRecordset.Fields(18 - 1) = ppp(2)
  68.           adoRecordset.Fields(19 - 1) = .TextString
  69.           adoRecordset.Fields(20 - 1) = .Alignment
  70.           'adoRecordset.Fields(21 - 1) = .UpsideDown
  71.           adoRecordset.Fields(21 - 1) = .Visible
  72.      '     ExcelAndMdbData.xlSheet.Cells(ii, 0) = .ObjectName
  73.           ii = ii + 1
  74.       End Select
  75.       
  76.     End With
  77.    
  78.   Next Ent
  79.   Debug.Print Time
  80.   rs.Fields.Append "aa", adBSTR
  81.   rs.Open
  82.   rs.AddNew
  83.   rs.Fields(0).Value = "aaaa"
  84.   'ExcelAndMdbData.xlSheet.Cells(2, 1).CopyFromRecordset rs
  85.   ExcelAndMdbData.xlSheet.Range("A2").CopyFromRecordset adoRecordset
  86.   Debug.Print Time()
  87. End Function
[/code]
  1. Sub ls()
  2.   Dim xlApp As Excel.Application
  3.   Dim xlBook As Excel.Workbook
  4.   Dim xlSheet As Excel.Worksheet
  5.   Set xlApp = GetObject(, "Excel.Application") '创建EXCEL对象
  6.   
  7.   'Set xlBook = xlApp.Workbooks.Open("d:\Attribute.xls") '打开已经存
  8.   xlApp.Visible = True '设置EXCEL对象可见(或不可见)
  9.   Set xlSheet = xlApp.ActiveWorkbook.Sheets("sheet2")    'xlBook.Worksheets("Sheet2") '设置活动工作表,sheet1表示表名,可以使用字符型变量代替。
  10.   xlSheet.Activate '激活工作表,让它处于前台活动中。
  11.   Dim FileTitle
  12.   FileTitle = Array("LineStartPoint0", "LineStartPoint1", "LineStartPoint2", "LineEndPoint0", "LineEndPoint1", "LineEndPoint2")
  13.   For ii = 0 To UBound(FileTitle)
  14.     xlSheet.Cells(1, ii + 1) = FileTitle(ii)
  15.   Next ii
  16.   Dim Ent As AcadEntity, EntLine As AcadLine
  17.   Dim RowCount As Integer
  18.   RowCount = 2
  19.   For Each Ent In ThisDrawing.ModelSpace
  20.     Select Case Ent.ObjectName
  21.       Case "AcDbLine"
  22.          Set EntLine = Ent
  23.          For ii = 0 To 2
  24.            xlSheet.Cells(RowCount, ii + 1) = EntLine.StartPoint(ii)
  25.            xlSheet.Cells(RowCount, ii + 4) = EntLine.EndPoint(ii)
  26.          Next
  27.     End Select
  28.     RowCount = RowCount + 1
  29.   Next
  30. End Sub
回复

使用道具 举报

20

主题

105

帖子

6

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
185
发表于 2008-3-27 22:30:00 | 显示全部楼层
有注解就好了,研究一下
回复

使用道具 举报

25

主题

474

帖子

12

银币

中流砥柱

Rank: 25

铜币
572
发表于 2008-3-28 12:05:00 | 显示全部楼层
能否把dvb文件传上来
回复

使用道具 举报

25

主题

474

帖子

12

银币

中流砥柱

Rank: 25

铜币
572
发表于 2008-3-28 12:05:00 | 显示全部楼层
能否把dvb文件传上来
回复

使用道具 举报

120

主题

326

帖子

7

银币

中流砥柱

Rank: 25

铜币
806
发表于 2008-3-28 13:31:00 | 显示全部楼层

用VB编的,有基本图形类模块和excel And Access类模块组成。
在VBA程序同样运行,只是将BaseGraphic.obj_ModelSpace改为Thisdrawing.ModelSpace而宜。
在此,给各位大侠出道题,就是如何将上面的程序,自己能够随心所欲得到应用。
1、AutoCAD+VBA与Excel有几种通迅方式:ADO,DAO,open
2、数据集的建立方式:SQL方式,以及实体变量数据直接赋值到数据集方式。如:
Dim TextRecordSet As ADODB.Recordset
Set TextRecordSet = New ADODB.Recordset
'设置Text数据集字段
  For jj = 0 To 22
    TextRecordSet.Fields.Append TextTitleVar(jj), adBSTR
  Next jj
  TextRecordSet.Open
  TextRecordSet.AddNew   '加n条记录
谢谢各位大侠的捧场,希望大家积极参与共同提高。
结论:CopyFromRecordset将采集到AutoCAD的实体数据,用数据集方式,将数据传递到excel,Access是一种比较快的数据传递方法。
回复

使用道具 举报

8

主题

31

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
63
发表于 2008-3-28 20:14:00 | 显示全部楼层
收下
学习一下
回复

使用道具 举报

0

主题

4

帖子

2

银币

初来乍到

Rank: 1

铜币
4
发表于 2008-4-1 22:36:00 | 显示全部楼层
楼主真牛
回复

使用道具 举报

0

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
1
发表于 2008-4-18 11:19:00 | 显示全部楼层
没看懂!
回复

使用道具 举报

0

主题

2

帖子

1

银币

初来乍到

Rank: 1

铜币
2
发表于 2008-4-19 09:03:00 | 显示全部楼层
好好学习一下
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-7-4 22:15 , Processed in 0.346837 second(s), 71 queries .

© 2020-2025 乐筑天下

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