找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1343|回复: 11

[VBA程序]:利用VBA编程实现从EXCEL表到AUTOCAD表转换

[复制链接]
发表于 2003-9-3 20:13:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  1. '                ExcelToCad
  2. '
  3. '**********************************************************************
  4. '原作:
  5. '        计算机世界报  新疆交通科学研究所  查拥军
  6. '                "利用VBA编程实现从EXCEL表到AUTOCAD表转换"
  7. '**********************************************************************
  8. '改编:
  9. '        zhqchn
  10. '**********************************************************************
  11. '简介:
  12. '            如何将Excel的制表计算与AutoCad的绘图功能结合,是很多人关心的问题
  13. '        Atable的出现以及unionsoft的TrueTable的迅速成熟为大家解决了大问题
  14. '        本人曾动过开发类似软件的念头,但接触TrueTable后就觉得没有必要了。
  15. '            前段时间看到有网友将查拥军的这篇文章贴到论坛,引来了不少关注,
  16. '        说明还是有不少人不满足于现成的工具,希望搞清原理,而查先生的程序
  17. '        只是摘录,也可能是故意隐去一些东西,因此不能直接运行,我将此程序
  18. '        做了修正,并加了一些注释,使有兴趣的初学者更容易调试,便于学习。
  19. '            我只是在原程序的基础是修正一些错误,程序还有很多不完善的地方,
  20. '        水平有限,高手莫笑话。
  21. '            程序请从cmdTableToExcel_Click运行,hxw中的Call wz(c, strText)
  22. '        是控制文字样式的,若打开,程序运行会很慢。
  23. '***********************************************************************
  24. '

  25. Option Explicit


  26. Private Sub cmdTableToExcel_Click()
  27.     Call ConnectCAD
  28.     Call ConnectEXCEL
  29.     Call tableToExcel
  30. End Sub

  31. Sub tableToExcel()
  32.     Call hxw
  33. End Sub

  34.       '---- 在AutoCAD中,线条有多种,考虑能够方便控制线条属性,选用了多义线。具体命令如下: RetVal =object.AddLightWeightPolyline (VerticesList)
  35.       '---- 下面的程序演示表格线条读取和画表格线的具体过程。
  36. Sub hxw()
  37.     Dim a As Integer '表格的最大行数
  38.     Dim b As Integer '表格的最大列数
  39.     Dim xinit As Double '插入点x坐标
  40.     Dim yinit As Double '插入点y坐标
  41.     Dim zinit As Double '插入点z坐标
  42.     Dim xinsert As Double '当前单元格的左上角点的x左标
  43.     Dim yinsert As Double '当前单元格的左上角点的y左标
  44.     Dim ptarray(0 To 3) As Double   '画表格线(lwpolyline)的顶点坐标
  45.     Dim X As Integer    '循环变量(表格行)
  46.     Dim Y As Integer    '循环变量(表格列)
  47.    
  48.     Dim c As Object     '单元格地址
  49.     Dim ma As Object    '合并单元格
  50.     Dim xl As Variant
  51.     Dim xh As Integer   '单元格宽
  52.     Dim yh As Integer   '单元格高
  53.     Dim xlRange As Object
  54.     Dim xpoint As Double    '当前单元格的左上角点的x左标(已加上插入点x坐标)
  55.     Dim ypoint As Double    '当前单元格的左上角点的y左标(已加上插入点y坐标)
  56.     Dim lwployobj As AcadLWPolyline '多义线
  57.     Dim textobj As AcadMText         '文本
  58.     Dim fptText(0 To 2) As Double   '文本插入点
  59.     Dim strText As String           '文本内容
  60.     Dim txtHeight As Double         '文本高度
  61.    
  62.     a = xlApp.Selection.Rows.Count
  63.     b = xlApp.Selection.Columns.Count
  64.    
  65.    
  66.     For X = 1 To a
  67.         For Y = 1 To b
  68.             Set c = xlSht.Range(zh(Y) + Trim(str(X)))
  69.             '以行号、列号获得单元格地址
  70.             Set ma = c.MergeArea
  71.             '求出单元格C的合并单元格地址
  72.             Dim tempStr As String
  73.             tempStr = Trim(ma.Address)
  74.             If ma.Count > 1 Then tempStr = Divide(Trim(ma.Address), ":", 0)
  75.             If tempStr = Trim(c.Address) Then
  76.                 '假如c.mergearea的绝对地址 , 如果前4个字符与c单元格的地址相同
  77.                 xl = "A1:" + ma.Address
  78.                 xh = xlSht.Range(ma.Address).Width
  79.                 yh = xlSht.Range(ma.Address).Height
  80.                 Set xlRange = xlSht.Range(xl)
  81.                 xinsert = xlRange.Width - xh
  82.                 yinsert = xlRange.Height - yh
  83.                 xpoint = xinit + xinsert
  84.                 ypoint = yinit - yinsert
  85.                 If X = 1 Then               '第一行
  86.                     If ma.Borders(xlEdgeTop).LineStyle <> xlNone Then
  87.                         ptarray(0) = xpoint
  88.                         '第一点坐标(数组下标 0 and 1)
  89.                         ptarray(1) = ypoint
  90.                         ptarray(2) = xpoint + xh
  91.                         '第二点坐标(数组下标 2 and 3)
  92.                         ptarray(3) = ypoint
  93.                         
  94.                         Set lwployobj = moSpace.AddLightWeightPolyline(ptarray)
  95.                         '在AutoCAD文件里画线
  96.                         With lwployobj
  97.                             '.Layer = newlayer.Name '指定lwployobj所在图层
  98.                             .Color = acBlue   '指定lwployobj的颜色
  99.                         End With
  100.                         Lineweight lwployobj, ma.Borders(xlEdgeTop).Weight
  101.                         lwployobj.Update
  102.                     End If
  103.                 End If
  104.                 If ma.Borders(xlEdgeBottom).LineStyle <> xlNone Then
  105.                     ptarray(0) = xpoint + xh
  106.                     '第三点坐标(数组下标 0 and 1)
  107.                     ptarray(1) = ypoint - yh
  108.                     ptarray(2) = xpoint
  109.                     '第四点坐标(数组下标 2 and 3)
  110.                      ptarray(3) = ypoint - yh
  111.                     Set lwployobj = moSpace.AddLightWeightPolyline(ptarray)
  112.                     '在AutoCAD文件里画线
  113.                     With lwployobj
  114.                         '.Layer = newlayer.Name '指定lwployobj所在图层
  115.                         .Color = acBlue   '指定lwployobj的颜色
  116.                     End With
  117.                     Lineweight lwployobj, ma.Borders(xlEdgeBottom).Weight
  118.                     lwployobj.Update
  119.                 End If
  120.                
  121.                
  122.                
  123.                 If Y = 1 Then               '第一列
  124.                     If ma.Borders(xlEdgeLeft).LineStyle <> xlNone Then
  125.                        ptarray(0) = xpoint
  126.                        '第四点坐标(数组下标 0 and 1)
  127.                        ptarray(1) = ypoint - yh
  128.                        ptarray(2) = xpoint
  129.                        '第一点坐标(数组下标 2 and 3)
  130.                         ptarray(3) = ypoint
  131.                         
  132.                         Set lwployobj = moSpace.AddLightWeightPolyline(ptarray)
  133.                         '在AutoCAD文件里画线
  134.                         With lwployobj
  135.                             '.Layer = newlayer.Name '指定lwployobj所在图层
  136.                             .Color = acBlue   '指定lwployobj的颜色
  137.                         End With
  138.                         Lineweight lwployobj, ma.Borders(xlEdgeLeft).Weight
  139.                         lwployobj.Update
  140.                     End If
  141.                 End If
  142.                 If ma.Borders(xlEdgeRight).LineStyle <> xlNone Then
  143.                     ptarray(0) = xpoint + xh
  144.                     '第二点坐标(数组下标 0 and 1)
  145.                     ptarray(1) = ypoint
  146.                     ptarray(2) = xpoint + xh
  147.                     '第三点坐标(数组下标 2 and 3)
  148.                     ptarray(3) = ypoint - yh
  149.                     
  150.                     Set lwployobj = moSpace.AddLightWeightPolyline(ptarray)
  151.                     '在AutoCAD文件里画线
  152.                     With lwployobj
  153.                         '.Layer = newlayer.Name '指定lwployobj所在图层
  154.                         .Color = acBlue   '指定lwployobj的颜色
  155.                     End With
  156.                     Lineweight lwployobj, ma.Borders(xlEdgeRight).Weight
  157.                     lwployobj.Update
  158.                     
  159.                     txtHeight = c.Font.Size * 2 / 3     '字高
  160.                     fptText(0) = xpoint
  161.                     fptText(1) = ypoint - yh / 2 + txtHeight / 2
  162.                     fptText(2) = 0
  163.                     strText = c.Text

  164.                     Call wz(c, strText)
  165.                     
  166.                     Set textobj = moSpace.AddMText(fptText, xh, strText)
  167.                     
  168.                     Call kz(textobj, c)
  169.                     
  170.                 End If
  171.             End If                  '??????????????????
  172.         Next Y
  173.     Next X
  174. End Sub
  175. '下面程序控制线条粗细
  176. Sub Lineweight(ByVal line As Object, u As Integer)
  177.     Select Case u
  178.         Case 1
  179.             Call line.SetWidth(0, 0.1, 0.1)
  180.         Case 2
  181.             Call line.SetWidth(0, 0.3, 0.3)
  182.         Case -4138
  183.             Call line.SetWidth(0, 0.5, 0.5)
  184.         Case 4
  185.             Call line.SetWidth(0, 1, 1)
  186.         Case Else
  187.             Call line.SetWidth(0, 0.1, 0.1)
  188.     End Select
  189. End Sub
  190. '下面程序完成列号转换
  191. Function zh(pp As Integer) As String
  192.     If pp <= 26 Then
  193.         zh = Chr(64 + pp)
  194.     Else
  195.         If (pp Mod 26) = 0 Then
  196.             zh = Chr(64 + Int(pp / 26) - 1) + Chr(64 + 26)
  197.         Else
  198.             zh = Chr(64 + Int(pp / 26)) + Chr(64 + pp Mod 26)
  199.         End If
  200.     End If
  201. End Function


  202. Sub wz(c As Range, textStr)
  203.     Dim Char As String
  204.     Dim j As Integer
  205.     Dim cpt As String
  206.     Dim tempStr As String
  207.     Dim sonstr1 As String
  208.     Dim sonstr As String
  209.    
  210.     Char = RTrim(Left(c.Characters.Caption, 256))
  211.     If Char <> Empty Then
  212.         textStr = ""
  213.         For j = 1 To Len(Char)
  214.             If c.Characters(j, 1).Font.Underline = xlUnderlineStyleNone Then
  215.                 cpt = c.Characters(j, 1).Caption
  216.                 sonstr = ForeFontStr(c, j)
  217.                 tempStr = ""
  218.                 Do While j + 1 <= Len(Char)
  219.                      sonstr1 = ForeFontStr(c, j + 1)
  220.                      If sonstr1 = sonstr Then
  221.                         j = j + 1
  222.                         tempStr = tempStr + c.Characters(j, 1).Caption
  223.                      Else
  224.                         Exit Do
  225.                      End If
  226.                 Loop
  227.                 textStr = textStr + "{" + sonstr + cpt + tempStr + "}"
  228.             Else
  229.                 cpt = c.Characters(j, 1).Caption
  230.                 sonstr = ForeFontStr(c, j)
  231.                 tempStr = ""
  232.                 Do While j + 1 <= Len(Char)
  233.                     sonstr1 = ForeFontStr(c, j + 1)
  234.                     If sonstr1 = sonstr Then
  235.                        j = j + 1
  236.                        tempStr = tempStr + c.Characters(j, 1).Caption
  237.                     Else
  238.                        Exit Do
  239.                     End If
  240.                 Loop
  241.                 'textStr = textStr + "{\L" + sonstr(cpt) + tempStr + "\l}"        '????
  242.                 textStr = textStr + "{\L" + sonstr + cpt + tempStr + "\l}"      '????
  243.             End If
  244.         Next j
  245.     End If
  246. End Sub
  247. '下面函数控制字体本身属性
  248. Function ForeFontStr(m As Range, u As Integer) As String
  249.     Dim a1 As String: Dim a2 As String: Dim a3 As String
  250.     Dim a4 As String: Dim a5 As String: Dim a6 As String
  251.     a1 = "\F" + m.Characters(u, 1).Font.Name + ";"  '字体
  252.     a2 = IIf(m.Characters(u, 1).Font.Superscript = True, "\H0.33x;\A2;", "") '上脚标
  253.     a3 = IIf(m.Characters(u, 1).Font.Subscript = True, "\H0.33x;\A0;", "") '下脚标
  254.     a4 = IIf(m.Characters(u, 1).Font.FontStyle = "倾斜", "\Q18;", "") '倾斜
  255.     a5 = IIf(m.Characters(u, 1).Font.FontStyle = "加粗", "\W1.2;", "")  '加粗
  256.     a6 = IIf(m.Characters(u, 1).Font.FontStyle = "加粗 倾斜", "\W1.2;\Q18;", "") ' 加粗倾斜
  257.     ForeFontStr = a1 + a2 + a3 + a4 + a5 + a6
  258. End Function

  259.      ' ---- (2).表格中表格文字位置的转换
  260.      ' ---- 对文字对象的属性的直接控制来实现,通过with….end with
  261.      ' 结构可以很容易地控制文字的高度、图层、颜色、书写方向。由于Mtext文字提供支持的排列位置分为9种,必须根据Microsoft
  262.      ' Excel表格文字的排列方式加以合适的判定,然后进行转换。其具体的实现方法详见下面的程序。
  263. Sub kz(textobj, c)
  264.     With textobj '文字对象
  265.         Dim ma As Object
  266.         Set ma = c.MergeArea
  267.         .Height = c.Font.Size * 2 / 3       'Excel文字高度换算成Acad文字高度
  268.         '.Layer = newlayer.Name  '设置图层
  269.         .Color = acRed          '设置颜色
  270.         .DrawingDirection = 1    '设置书写方向
  271.        If (ma.VerticalAlignment = xlTop _
  272.            Or ma.VerticalAlignment = xlGeneral) _
  273.            And (ma.HorizontalAlignment = xlLeft _
  274.            Or ma.HorizontalAlignment = xlGeneral) _
  275.            Then .AttachmentPoint = 1  'acAttachmentPointTopLeft
  276.        If (ma.VerticalAlignment = xlTop _
  277.            Or ma.VerticalAlignment = xlGeneral) _
  278.            And (ma.HorizontalAlignment = xlCenter _
  279.            Or ma.HorizontalAlignment = xlJustify _
  280.            Or ma.HorizontalAlignment = xlDistributed) _
  281.            Then .AttachmentPoint = 2  'acAttachmentPointTopCenter
  282.        If (ma.VerticalAlignment = xlTop _
  283.            Or ma.VerticalAlignment = xlGeneral) _
  284.            And ma.HorizontalAlignment = xlRight _
  285.            Then .AttachmentPoint = 3  'acAttachmentPointTopRight
  286.        If (ma.VerticalAlignment = xlCenter _
  287.            Or ma.VerticalAlignment = xlJustify _
  288.            Or ma.VerticalAlignment = xlDistributed) _
  289.            And (ma.HorizontalAlignment = xlLeft _
  290.            Or ma.HorizontalAlignment = xlGeneral) _
  291.            Then .AttachmentPoint = 4  'acAttachmentPointMiddleLeft
  292.        If (ma.VerticalAlignment = xlCenter _
  293.            Or ma.VerticalAlignment = xlJustify _
  294.            Or ma.VerticalAlignment = xlDistributed) _
  295.            And (ma.HorizontalAlignment = xlCenter _
  296.            Or ma.HorizontalAlignment = xlJustify _
  297.            Or ma.HorizontalAlignment = xlDistributed) _
  298.            Then .AttachmentPoint = 5  'acAttachmentPointMiddleCenter
  299.        If (ma.VerticalAlignment = xlCenter _
  300.            Or ma.VerticalAlignment = xlJustify _
  301.            Or ma.VerticalAlignment = xlDistributed) _
  302.            And ma.HorizontalAlignment = xlRight _
  303.            Then .AttachmentPoint = 6 'acAttachmentPointMiddleRight
  304.        If ma.VerticalAlignment = xlBottom _
  305.            And (ma.HorizontalAlignment = xlLeft _
  306.            Or ma.HorizontalAlignment = xlGeneral) _
  307.            Then .AttachmentPoint = 7  'acAttachmentPointBottomLeft
  308.       If ma.VerticalAlignment = xlBottom _
  309.            And (ma.HorizontalAlignment = xlCenter _
  310.            Or ma.HorizontalAlignment = xlJustify _
  311.            Or ma.HorizontalAlignment = xlDistributed) _
  312.            Then .AttachmentPoint = 8  'acAttachmentPointBottomCenter
  313.       If ma.VerticalAlignment = xlBottom _
  314.            And ma.HorizontalAlignment = xlRight _
  315.            Then .AttachmentPoint = 9  'acAttachmentPointBottomRight
  316.     End With
  317.     textobj.Update
  318. End Sub






这是Excel表格
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
 楼主| 发表于 2003-9-3 20:18:54 | 显示全部楼层
这是画到Cad中的情况
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-9-16 15:40:13 | 显示全部楼层
合并单元格是这一块的难点,这个问题能够解决,很好,顶一下。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-11-30 04:21:19 | 显示全部楼层
不好意思,我是新手。程序运行的时候,怎么会出现错误?说  "变量为定义",怎么回事?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-8-19 00:37:23 | 显示全部楼层
谢谢了,我才学习编程
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2005-11-13 16:40:14 | 显示全部楼层
Call ConnectCAD
    Call ConnectEXCEL

这两个函数没有定义。请问如何解决?
是不是代码也不全?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2005-11-17 08:45:36 | 显示全部楼层
如果真要学习的话,可以去看本人在本版所发帖子“excel表格转cad(速度快20倍)”,里面的代码在excel单元格处理以及速度方面都要略胜一筹,你可以对比两者的速度及效果。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2005-11-17 13:55:59 | 显示全部楼层
这个帖子“excel表格转cad(速度快20倍)”,真的很不错
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2006-5-28 00:23:24 | 显示全部楼层
楼主能作个加载运行的动画吗,好像不能运行,提示:编译错误,用户定义类型未定义
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2006-7-23 01:13:36 | 显示全部楼层
7楼的帖子还要收钱呀。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2007-7-6 08:57:43 | 显示全部楼层
翻译成vlisp的代码就好了,楼上的程序能够合并单元格,非常好,blisp中能够合并单元格,设置对齐等格式就好了
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2007-7-31 08:53:05 | 显示全部楼层
我原下载过这么一个程序,可以把EXCEL表格转换成CAD中的表格,但因为知识产权的原因,我们不能用微软的EXCEL了,只能用金山的EXCEL,而我下载的那个把EXCEL表格转换成CAD中的表格的工具就不能用了,不知这个程序有办法吗
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-28 13:17 , Processed in 0.381643 second(s), 53 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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