明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 33815|回复: 62

Excel表格到CAD的示例程序

    [复制链接]
发表于 2004-2-11 19:31 | 显示全部楼层 |阅读模式
增加了一小部分注释,希望对大家有所帮助。
  1. Sub Test()
  2.        On Error Resume Next        ' 连接Excel应用程序       Dim xlApp As Excel.Application
  3.        Set xlApp = GetObject(, "Excel.Application")
  4.        If Err Then
  5.                MsgBox " Excel 应用程序没有运行。请启动 Excel 并重新运行程序。"
  6.                Exit Sub
  7.        End If
  8.        Dim xlSheet As Worksheet
  9.        Set xlSheet = xlApp.ActiveSheet       ' 当初考虑将表格做成块的方式,可以根据需要取舍。
  10.        'Dim iPt(0 To 2) As Double
  11.        'iPt(0) = 0: iPt(1) = 0: iPt(2) = 0
  12.        Dim BlockObj As AcadBlock
  13.        Set BlockObj = ThisDrawing.Blocks("*Model_Space")
  14.        Dim iPt As Variant
  15.        iPt = ThisDrawing.Utility.GetPoint(, "指定表格的插入点: ")
  16.        If IsEmpty(iPt) Then Exit Sub
  17.        Dim xlRange As Range
  18.        Debug.Print xlSheet.UsedRange.Address
  19.        For Each xlRange In xlSheet.UsedRange
  20.                AddLine BlockObj, iPt, xlRange
  21.                AddText BlockObj, iPt, xlRange
  22.        Next
  23.        Set xlRange = Nothing
  24.        Set xlSheet = Nothing
  25.        Set xlApp = Nothing
  26. End Sub
  27. '边框线条粗细
  28. Function LineWidth(ByVal xlBorder As Border) As Double
  29.        Select Case xlBorder.Weight
  30.                Case xlThin
  31.                        LineWidth = 0
  32.                Case xlMedium
  33.                        LineWidth = 0.35
  34.                Case xlThick
  35.                        LineWidth = 0.7
  36.                Case Else
  37.                        LineWidth = 0
  38.        End Select
  39. End Function
  40. '边框线条颜色,处理的颜色不全,请自己添加
  41. Function LineColor(ByVal xlBorder As Border) As Integer
  42.        Select Case xlBorder.ColorIndex
  43.                Case xlAutomatic
  44.                        LineColor = acByLayer
  45.                Case 3
  46.                        LineColor = acRed
  47.                Case 4
  48.                        LineColor = acGreen
  49.                Case 5
  50.                        LineColor = acBlue
  51.                Case 6
  52.                        LineColor = acYellow
  53.                  Case 8
  54.                        LineColor = acCyan
  55.                  Case 9
  56.                        LineColor = acMagenta
  57.                Case Else
  58.                        LineColor = acByLayer
  59.        End Select
  60. End Function
  61. '给制边框
  62. Sub AddLine(ByRef BlockObj As AcadBlock, ByVal iPt As Variant, ByVal xlRange As Range)
  63.        If xlRange.Borders(xlEdgeLeft).LineStyle = xlNone _
  64.                And xlRange.Borders(xlEdgeBottom).LineStyle = xlNone _
  65.                And xlRange.Borders(xlEdgeRight).LineStyle = xlNone _
  66.                And xlRange.Borders(xlEdgeTop).LineStyle = xlNone Then Exit Sub
  67.        Dim rl As Double
  68.        Dim rt As Double
  69.        Dim rw As Double
  70.        Dim rh As Double
  71.        rl = PToM(xlRange.Left)
  72.        rt = PToM(xlRange.top)
  73.        rw = PToM(xlRange.Width)
  74.        rh = PToM(xlRange.Height)
  75.        Dim pPt(0 To 3) As Double
  76.        Dim pLineObj As AcadLWPolyline       ' 左边框的处理,仅第一列才做处理。
  77.        If xlRange.Borders(xlEdgeLeft).LineStyle <> xlNone And xlRange.Column = 1 Then
  78.                pPt(0) = iPt(0) + rl: pPt(1) = iPt(1) - rt
  79.                pPt(2) = iPt(0) + rl: pPt(3) = iPt(1) - (rt + rh)
  80.                Set pLineObj = BlockObj.AddLightWeightPolyline(pPt)
  81.                pLineObj.ConstantWidth = LineWidth(xlRange.Borders(xlEdgeLeft))
  82.                pLineObj.Color = LineColor(xlRange.Borders(xlEdgeLeft))
  83.        End If       ' 下边框的处理,对于合并单元格,只处理最后一行。
  84.        If xlRange.Borders(xlEdgeBottom).LineStyle <> xlNone And (xlRange.Row = xlRange.MergeArea.Row + xlRange.MergeArea.Rows.Count - 1) Then
  85.                pPt(0) = iPt(0) + rl: pPt(1) = iPt(1) - (rt + rh)
  86.                pPt(2) = iPt(0) + rl + rw: pPt(3) = iPt(1) - (rt + rh)
  87.                Set pLineObj = BlockObj.AddLightWeightPolyline(pPt)
  88.                pLineObj.ConstantWidth = LineWidth(xlRange.Borders(xlEdgeBottom))
  89.                pLineObj.Color = LineColor(xlRange.Borders(xlEdgeBottom))
  90.        End If       ' 右边框的处理,对于合并单元格,只处理最后一列。
  91.        If xlRange.Borders(xlEdgeRight).LineStyle <> xlNone And (xlRange.Column >= xlRange.MergeArea.Column + xlRange.MergeArea.Columns.Count - 1) Then
  92.                pPt(0) = iPt(0) + rl + rw: pPt(1) = iPt(1) - (rt + rh)
  93.                pPt(2) = iPt(0) + rl + rw: pPt(3) = iPt(1) - rt
  94.                Set pLineObj = BlockObj.AddLightWeightPolyline(pPt)
  95.                pLineObj.ConstantWidth = LineWidth(xlRange.Borders(xlEdgeRight))
  96.                pLineObj.Color = LineColor(xlRange.Borders(xlEdgeRight))
  97.        End If       ' 上边框的处理,仅第一行才做处理。
  98.        If xlRange.Borders(xlEdgeTop).LineStyle <> xlNone And xlRange.Row = 1 Then
  99.                pPt(0) = iPt(0) + rl + rw: pPt(1) = iPt(1) - rt
  100.                pPt(2) = iPt(0) + rl: pPt(3) = iPt(1) - rt
  101.                Set pLineObj = BlockObj.AddLightWeightPolyline(pPt)
  102.                pLineObj.ConstantWidth = LineWidth(xlRange.Borders(xlEdgeTop))
  103.                pLineObj.Color = LineColor(xlRange.Borders(xlEdgeTop))
  104.        End If
  105.        Set pLineObj = Nothing
  106. End Sub
  107. '给制文本
  108. Sub AddText(ByRef BlockObj As AcadBlock, ByVal InsertionPoint As Variant, ByVal xlRange As Range)
  109.        If xlRange.Text = "" Then Exit Sub
  110.        Dim rl As Double
  111.        Dim rt As Double
  112.        Dim rw As Double
  113.        Dim rh As Double
  114.        rl = PToM(xlRange.Left)
  115.        rt = PToM(xlRange.top)
  116.        rw = PToM(xlRange.MergeArea.Width)
  117.        rh = PToM(xlRange.MergeArea.Height)
  118.        Dim i As Integer
  119.        Dim s As String
  120.        For i = 1 To Len(xlRange.Text) '将EXCEL的换行符替换成\P,注如果是在R2002以上可使用Replace函数。
  121.                If Asc(Mid(xlRange.Text, i, 1)) = 10 Then
  122.                        s = s & "\P"
  123.                Else
  124.                        s = s & Mid(xlRange.Text, i, 1)
  125.                End If
  126.        Next
  127.        Dim iPt(0 To 2) As Double
  128.        iPt(0) = InsertionPoint(0) + rl: iPt(1) = InsertionPoint(1) - rt: iPt(2) = 0
  129.        Dim mTextObj As AcadMText
  130.        Set mTextObj = BlockObj.AddMText(iPt, rw, s)   '"{\f" & xlRange.Font.Name & ";" & s & "}")
  131.        mTextObj.LineSpacingFactor = 0.75
  132.        mTextObj.Height = PToM(xlRange.Font.Size)       ' 处理文字的对齐方式
  133.        Dim tPt As Variant
  134.        If xlRange.VerticalAlignment = xlTop And (xlRange.HorizontalAlignment = xlLeft Or xlRange.HorizontalAlignment = xlGeneral) Then
  135.                mTextObj.AttachmentPoint = acAttachmentPointTopLeft
  136.                tPt = iPt
  137.        ElseIf xlRange.VerticalAlignment = xlTop And xlRange.HorizontalAlignment = xlCenter Then
  138.                mTextObj.AttachmentPoint = acAttachmentPointTopCenter
  139.                tPt = ThisDrawing.Utility.PolarPoint(iPt, 0, rw / 2)
  140.        ElseIf xlRange.VerticalAlignment = xlTop And xlRange.HorizontalAlignment = xlRight Then
  141.                mTextObj.AttachmentPoint = acAttachmentPointTopRight
  142.                tPt = ThisDrawing.Utility.PolarPoint(iPt, 0, rw)
  143.        ElseIf xlRange.VerticalAlignment = xlCenter And (xlRange.HorizontalAlignment = xlLeft _
  144.                        Or xlRange.HorizontalAlignment = xlGeneral) Then
  145.                mTextObj.AttachmentPoint = acAttachmentPointMiddleLeft
  146.                tPt = ThisDrawing.Utility.PolarPoint(iPt, -1.5707963, rh / 2)
  147.        ElseIf xlRange.VerticalAlignment = xlCenter And xlRange.HorizontalAlignment = xlCenter Then
  148.                mTextObj.AttachmentPoint = acAttachmentPointMiddleCenter
  149.                tPt = ThisDrawing.Utility.PolarPoint(iPt, -1.5707963, rh / 2)
  150.                tPt = ThisDrawing.Utility.PolarPoint(tPt, 0, rw / 2)
  151.        ElseIf xlRange.VerticalAlignment = xlCenter And xlRange.HorizontalAlignment = xlRight Then
  152.                mTextObj.AttachmentPoint = acAttachmentPointMiddleRight
  153.                tPt = ThisDrawing.Utility.PolarPoint(iPt, -1.5707963, rh / 2)
  154.                tPt = ThisDrawing.Utility.PolarPoint(tPt, 0, rw / 2)
  155.        ElseIf xlRange.VerticalAlignment = xlBottom And (xlRange.HorizontalAlignment = xlLeft _
  156.                        Or xlRange.HorizontalAlignment = xlGeneral) Then
  157.                mTextObj.AttachmentPoint = acAttachmentPointBottomLeft
  158.                tPt = ThisDrawing.Utility.PolarPoint(iPt, -1.5707963, rh)
  159.        ElseIf xlRange.VerticalAlignment = xlBottom And xlRange.HorizontalAlignment = xlCenter Then
  160.                mTextObj.AttachmentPoint = acAttachmentPointBottomCenter
  161.                tPt = ThisDrawing.Utility.PolarPoint(iPt, -1.5707963, rh)
  162.                tPt = ThisDrawing.Utility.PolarPoint(tPt, 0, rw / 2)
  163.        ElseIf xlRange.VerticalAlignment = xlBottom And xlRange.HorizontalAlignment = xlRight Then
  164.                mTextObj.AttachmentPoint = acAttachmentPointBottomRight
  165.                tPt = ThisDrawing.Utility.PolarPoint(iPt, -1.5707963, rh)
  166.                tPt = ThisDrawing.Utility.PolarPoint(tPt, 0, rw)
  167.        End If
  168.        mTextObj.InsertionPoint = tPt
  169.        Set mTextObj = Nothing
  170. End Sub
  171. ' 磅换算成毫米' 注:意义不大,转换的尺寸有偏差,最好自己设定一个转换规则。
  172. Function PToM(ByVal Points As Double) As Double
  173.        PToM = Points * 0.3527778
  174. End Function

评分

参与人数 1威望 +1 金钱 +10 贡献 +5 激情 +5 收起 理由
mccad + 1 + 10 + 5 + 5 【精华】好程序

查看全部评分

发表于 2022-4-7 13:08 | 显示全部楼层
谢谢楼主分享!
发表于 2004-2-20 13:25 | 显示全部楼层
坚决支持,虽然看不太懂
 楼主| 发表于 2004-2-25 21:56 | 显示全部楼层
  1. 表格处理模块详细设计作者:efan2000         
  2.                                                    
  3. 模块设计说明模块概述
  4. 产生背景:AutoCAD表格绘制、工程计算的功能十分的薄弱,但实际应用中,这两项功能是不可缺少的,因而,需要一种简便的方法来弥补这种缺陷。Excel是表格处理软件,带有强大的计算功能,而且开发方便,正好可以利用。因此,就可以通过Excel计算、制表,然后转化到AutoCAD来实现AutoCAD表格的生成。
  5. 平台环境:Excel、AutoCAD、VBA。
  6. 技术介绍:使用VBA,采用OLE方式对Excel、AutoCAD进行二次开发,实现功能的扩充。
  7. 功能说明:1、表格的边框处理模块:如边框的线型、颜色、线重等。
  8.                    2、表格的文字处理模块:如文字的字体、字号、颜色等。
复制代码
未完成...,欢迎大家来共同完善。
发表于 2004-3-12 17:31 | 显示全部楼层
很好的例子,我用了不知道是什么原因,转换后有很多"?"号


还有能否给给详细点的说明,我真的学这个,有些地方还看不懂,


转换规则应该怎样指定
发表于 2004-3-13 10:48 | 显示全部楼层
我熟悉CAD,Excel,Vlisp,可惜唯独不懂VBA。我也急需这种好程序,不知哪位大侠肯告诉我把上面的程序放到哪里,文件名后缀为什么,如何加载,加载后命名为什么。最后请高手指点VBA书籍中高,中,低级最好为哪三本。在下万分感激。 ----------------------------------------------
wyj_007@sina.com 隔行如隔山,此话真不假!但愿有仙人,渡我披新袈。
发表于 2004-3-19 22:23 | 显示全部楼层
好!请问VBA如何调用CAD里的color对话框?
发表于 2004-3-25 14:21 | 显示全部楼层
在调用EXCEL时,如果EXCEL没有打开可以通过错误处理程序执行代码,通过公用对话框让用户打开特定的文件,代码如下: On Error Resume Next
Set appexcel = GetObject(, "excel.Application")
If Err Then
Err.Clear
Set appexcel = CreateObject("excel.Application")
Set workbooks = appexcel.workbooks
Set workbook = workbooks.Add
Set worksheet = workbook.ActiveSheet
Else
Set workbook = appexcel.ActiveWorkbook
Set worksheet = workbook.Sheets("sheet1")
End If
发表于 2004-3-29 15:15 | 显示全部楼层
我经常使用这两个软件,数据、表格转换是常有的事,以前曾使用过维维软件的“报表转绘王”,但未注册用户一次只能转4行。


现在有了这段代码,真是太好了。
发表于 2004-4-1 09:57 | 显示全部楼层
谢谢!尽管我不大懂。
发表于 2004-4-10 11:36 | 显示全部楼层
"rl = PToM(xlRange.Left)
rt = PToM(xlRange.top)
rw = PToM(xlRange.Width)
rh = PToM(xlRange.Height)"
xlRange.Height得到的数据,有时并不会由行高变化而变化!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-26 19:10 , Processed in 3.489482 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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