明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: alex

CAD表格转换至Excel

  [复制链接]
发表于 2007-10-30 13:01:00 | 显示全部楼层

Public xlApp As Excel.Application
Public xlWork As Excel.Workbook
Public xlSheet As Excel.Worksheet

Function OpenExcel() As Boolean
  On Error Resume Next
  If xlApp Is Nothing Then
    Set xlApp = GetObject(, "Excel.Application")
  End If
  If xlApp Is Nothing Then
'  Set xlApp = GetObject(, "Excel.Application")
'  If Err.Number <> 0 Then
    On Error GoTo OpenExcelFaild
    Set xlApp = CreateObject("Excel.Application")
  End If
  OpenExcel = True
  Exit Function
OpenExcelFaild:
  OpenExcel = False
End Function


Private Sub cmdPickup_Click()
    On Error Resume Next
    ThisDrawing.SelectionSets.Item("sHLine").Delete
    ThisDrawing.SelectionSets.Item("sVLine").Delete
    ThisDrawing.SelectionSets.Item("ss").Delete
    On Error GoTo 0
    Dim pt(0 To 2) As Double, pt1(0 To 2) As Double
    Dim seldata(0) As Variant, selcode(0) As Integer
    Dim gpdata As Variant, gpcode As Variant
    Dim ret As Variant
    Dim sel As AcadSelectionSet
    Dim shl As AcadSelectionSet, svl As AcadSelectionSet
    Dim i As Long, j As Long
    Dim obj As AcadEntity, objLine As AcadLine
    Dim minpt As Variant, maxpt As Variant
    Dim addobj(0) As AcadEntity
    Dim x As Double, y As Double, x0 As Double
    Dim x1 As Double, y1 As Double
    Dim n1 As Long, n2 As Long
    Dim s As String, np As Long, nf As Long
    With ThisDrawing
        Me.Hide
        ret = .Utility.GetPoint(, "指定左上角:")
        SetRet ret, pt
        ret = .Utility.GetCorner(pt, "指定对角点:")
        SetRet ret, pt1
        Set sel = .SelectionSets.Add("ss")
        selcode(0) = 0: gpcode = selcode
        seldata(0) = "Line": gpdata = seldata
        sel.Select acSelectionSetCrossing, pt, pt1, gpcode, gpdata
        lv.ListItems.Clear
        lv.ColumnHeaders.Clear
        Set shl = .SelectionSets.Add("sHLine")
        Set svl = .SelectionSets.Add("sVLine")
        For Each obj In sel
            obj.GetBoundingBox minpt, maxpt
            Set addobj(0) = obj
            If Abs(minpt(0) - maxpt(0)) > Abs(minpt(1) - maxpt(1)) Then
                shl.AddItems addobj
            Else
                svl.AddItems addobj
            End If
        Next
        Sort shl, 0
        Sort svl, 1
        n1 = shl.count
        n2 = svl.count
        lb.Caption = "选择了" & vbCrLf & n1 & "行水平线" & vbCrLf & n2 & "行垂直线"
        If n1 > 0 And n2 > 0 Then
            For i = 1 To n2
                lv.ColumnHeaders.Add , , "col" & i
            Next
            lv.ColumnHeaders.Item(1).Text = "No."
            lv.ColumnHeaders.Item(1).Width = 30
            Set obj = svl.Item(0)
            obj.GetBoundingBox minpt, maxpt
            x0 = minpt(0)
            If x0 > pt(0) Then
                lv.ColumnHeaders.Add , , "col" & lv.ColumnHeaders.count + 1
                np = 1
                x0 = pt(0)
            End If
            Set obj = svl.Item(n2 - 1)
            obj.GetBoundingBox minpt, maxpt
            x = minpt(0)
            If x < pt1(0) Then
                lv.ColumnHeaders.Add , , "col" & lv.ColumnHeaders.count + 1
                nf = 1
            End If
            x = x0
            Set obj = shl.Item(0)
            obj.GetBoundingBox minpt, maxpt
            y = minpt(1)
            For i = 1 To n1 - 1
                lv.ListItems.Add , , i
                Set obj = shl.Item(i)
                obj.GetBoundingBox minpt, maxpt
                y1 = minpt(1)
                If np = 1 Then
                    Set obj = svl.Item(0)
                    obj.GetBoundingBox minpt, maxpt
                    x1 = minpt(0)
                    s = GetText(x, y, x1, y1)
                    lv.ListItems(i).SubItems(1) = s
                    x = x1
                End If
                For j = 1 To n2 - 1
                    Set obj = svl.Item(j)
                    obj.GetBoundingBox minpt, maxpt
                    x1 = minpt(0)
                    s = GetText(x, y, x1, y1)
                    lv.ListItems(i).SubItems(j + np) = s
                    x = x1
                Next
                If nf = 1 Then
                    x1 = pt1(0)
                    s = GetText(x, y, x1, y1)
                    lv.ListItems(i).SubItems(j + np) = s
                End If
    Debug.Print
                x = x0
                y = y1
            Next
        End If
               
        .SelectionSets.Item("sHLine").Delete
        .SelectionSets.Item("sVLine").Delete
        .SelectionSets.Item("ss").Delete
        Me.Show
       
    End With
End Sub

发表于 2007-10-30 13:04:00 | 显示全部楼层

本帖子中包含更多资源

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

x
发表于 2007-10-30 13:05:00 | 显示全部楼层
已经取消了密码
发表于 2007-11-9 15:06:00 | 显示全部楼层
很好,有点遗憾的是代码是vb编写,而不是vc,只好动手转成vc
发表于 2007-11-14 14:29:00 | 显示全部楼层

请大家看一下贴子:《怎么样才能通过VBA程序将这些统计数据写入DBF文件中。(已建有相同字段的D:\CYC\DeckP.dbf文件)

帮忙解决一下燃眉之急,谢谢!

发表于 2007-11-22 11:34:00 | 显示全部楼层

很不错,只是有点遗憾,在单元格边线错开时,提取出来的文本就重复读取。

发表于 2007-12-25 14:40:00 | 显示全部楼层

专门研究此帖子。

发表于 2008-3-15 14:34:00 | 显示全部楼层
hhhhhhhhh
发表于 2008-4-26 13:50:00 | 显示全部楼层
好东西 ,学习 向楼主的无私致敬
发表于 2008-6-17 07:57:00 | 显示全部楼层

改进一下那就更好了,呵呵

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

本版积分规则

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

GMT+8, 2024-12-23 10:34 , Processed in 0.190346 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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