明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5351|回复: 15

[资源] 利用Excel的VBA将Excel表格导入CAD

  [复制链接]
发表于 2017-11-21 15:24:00 | 显示全部楼层 |阅读模式
本帖最后由 springwillow 于 2020-12-29 14:30 编辑
  1. Sub ExportToACAD()
  2.     On Error Resume Next
  3.     Dim objAcad As Object     ''AcadApplication
  4.     Dim objAcadDoc As Object
  5. ''AcadDocument
  6.     Dim objModelSpace As Object       ''AcadModelSpace
  7.    
  8. Dim msgResult As Integer
  9.     Dim a As Range
  10.     If Selection Is Nothing Then MsgBox "Nothing Selected!": Exit Sub
  11.     msgResult = MsgBox("您共选择了" & Selection.Rows.Count & "行 " & Selection.Columns.Count & "列," & Chr(13) & "请注意一些对齐方式可能被忽略!" & Chr(13) & "继续吗 ", vbOKCancel, "选择")
  12.     If msgResult = vbCancel Then Exit Sub
  13.     Err.Clear
  14.     Set objAcad = GetObject(, "AutoCAD.application")
  15.     If Err.Number = 0 Then GoTo Finish
  16.     Err.Clear
  17.     Set objAcad = CreateObject("autocad.application")
  18. Finish:
  19.     If Err.Number <> 0 Then
  20.         MsgBox "You must have AutoCAD installed to run this Macro!", vbCritical, "Export to ACAD"
  21.         Exit Sub
  22.     End If
  23.     On Error GoTo errHandler
  24.     Set objAcadDoc = objAcad.Documents.Add
  25.     Set objModelSpace = objAcadDoc.ModelSpace
  26.     Dim textObj As Object
  27. ''AcadText
  28.     Dim lineObj As Object       ''AcadLine
  29.     Dim insPnt(0 To 2) As Double
  30.     Dim stPnt(0 To 2) As Double
  31.     Dim edPnt(0 To 2) As Double
  32.     Dim txtHeight As Double
  33.     Const txtClearance As Double = 2
  34.     Static startY As Double
  35.     startY = Selection.Rows(Selection.Rows.Count).Top - Selection.Rows(1).Top
  36.     For Each a In Selection
  37.         If a.Borders(xlEdgeTop).LineStyle = xlContinuous Then
  38.             stPnt(0) = a.Left: stPnt(1) = startY - a.Top: stPnt(2) = 0
  39.             edPnt(0) = a.Left + a.Width: edPnt(1) = stPnt(1): edPnt(2) = 0
  40.             Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
  41.         
  42. End If
  43.         If a.Borders(xlEdgeLeft).LineStyle = xlContinuous Then
  44.             stPnt(0) = a.Left: stPnt(1) = startY - a.Top: stPnt(2) = 0
  45.             edPnt(0) = stPnt(0): edPnt(1) = startY - a.Top - a.Height:
  46. edPnt(2) = 0
  47.             Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
  48.         End If
  49.         txtHeight = a.Font.Size / 1.5
  50.         If Trim(a.Text) <> "" Then
  51.             If a.HorizontalAlignment = xlCenter Then
  52.                 insPnt(0) = a.Left + a.Width / 2
  53.                
  54. insPnt(1) = startY - a.Top - a.Height / 2
  55.                 insPnt(2) = 0
  56.                 Set textObj = objModelSpace.AddText(a.Text, insPnt, txtHeight)
  57.                 textObj.Alignment = 10
  58. 'acAlignmentMiddleCenter
  59.                 textObj.TextAlignmentPoint = insPnt
  60.             ElseIf a.HorizontalAlignment = xlLeft Or (a.HorizontalAlignment = xlGeneral And _
  61.                 Not IsNumeric(a.Text)) Then
  62.                 insPnt(0) = a.Left + txtClearance
  63.                 insPnt(1) = startY - a.Top - a.Height / 2
  64.                 insPnt(2) = 0
  65.                 Set textObj = objModelSpace.AddText(a.Text, insPnt, txtHeight)
  66.                
  67. textObj.Alignment = 9   'acAlignmentMiddleLeft
  68.                
  69. textObj.TextAlignmentPoint = insPnt
  70.             Else
  71.                
  72. insPnt(0) = a.Left + a.Width - txtClearance
  73.                 insPnt(1) = startY - a.Top - a.Height / 2
  74.                 insPnt(2) = 0
  75.                 Set textObj = objModelSpace.AddText(a.Text, insPnt, txtHeight)
  76.                 textObj.Alignment = 11
  77. 'acAlignmentMiddleRight
  78.                 textObj.TextAlignmentPoint = insPnt
  79.             End If
  80.         End If
  81.     Next a
  82.     For Each a In Selection.Offset(Selection.Rows.Count - 1, 0). _
  83.         Resize(1, Selection.Columns.Count)
  84.         If a.Borders(xlEdgeBottom).LineStyle = xlContinuous Then
  85.             stPnt(0) = a.Left: stPnt(1) = startY - a.Top - a.Height: stPnt(2) = 0
  86.             edPnt(0) = a.Left + a.Width: edPnt(1) = stPnt(1): edPnt(2) = 0
  87.             Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
  88.         End If
  89.     Next
  90.     For Each a In Selection.Offset(0, Selection.Columns.Count - 1). _
  91.         Resize(Selection.Rows.Count, 1)
  92.         If a.Borders(xlEdgeRight).LineStyle = xlContinuous Then
  93.             stPnt(0) = a.Left + a.Width: stPnt(1) = startY - a.Top:
  94. stPnt(2) = 0
  95.             edPnt(0) = stPnt(0): edPnt(1) = startY - a.Top - a.Height: edPnt(2) = 0
  96.             Set lineObj = objModelSpace.AddLine(stPnt, edPnt)
  97.         End If
  98.     Next
  99.     Application.WindowState = xlMinimized
  100.     objAcad.WindowState = 3 'acMax
  101.     objAcad.Visible = True
  102.     objAcad.ZoomAll
  103.     'objAcad.Close
  104.     Set objAcad = Nothing
  105.     Set objAcadDoc = Nothing
  106.     Set objModelSpace = Nothing
  107.     Exit Sub
  108. errHandler: MsgBox "在该系统中不能正常运行! " & Chr(10) & Err.Description, vbCritical, "Export to ACAD"
  109.     End Sub

使用方法是打开Excel程序,找到开发工具(如果没有可以在选项-自定义功能区中填加),点击Visual Basic按钮调出VBA编辑器,把代码粘贴到代码窗口。回到Excel,选中要插入CAD的单元格,设置好单元格的边框,执行宏即可。

本帖子中包含更多资源

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

x

评分

参与人数 1明经币 +1 收起 理由
yaokui25 + 1 很给力!

查看全部评分

"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2017-11-24 08:48:38 | 显示全部楼层
本帖最后由 sunny_8848 于 2017-11-24 08:49 编辑

要是能把那些每行的横向或者纵向的表格线分别绘制成一条条的就更好了,现在是按单元格绘制的。一般表格都是绘制多少行多少列的样式

点评

excel里复制,acad里选择性粘贴  发表于 2017-11-24 09:02
发表于 2017-11-22 11:06:54 | 显示全部楼层
谢楼主分享,已经加入我常用的工具箱中
发表于 2018-1-31 10:26:43 | 显示全部楼层
代码会出错!好像段落出现问题!
发表于 2017-11-21 18:39:00 | 显示全部楼层
谢谢楼主分享这么好的程序
发表于 2017-12-12 17:13:16 | 显示全部楼层
语法有错误呀
发表于 2017-12-16 09:02:29 | 显示全部楼层
语法有错误啊
 楼主| 发表于 2017-12-16 12:03:19 | 显示全部楼层
office2010运行没问题啊!
发表于 2018-3-15 10:30:42 | 显示全部楼层
谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-14 14:30 , Processed in 0.491954 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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