CAD VBA 导出cass扩展数据到excel

        cass中往往会写入扩展数据,但获取扩展数据较为麻烦,此例我们通过getxdata函数获取实体的扩展数据,然后逐要素循环写入excel中,代码如下:

版本1:要素扩展数据字段不同


Sub 导出扩展数据到excel()
    Dim Excel As Object
    Dim elem As Object
    Dim excelSheet As Object
    Dim Array1 As Variant
    Dim xdataOut As Variant
    Dim xtypeOut As Variant
    Dim Count, RowNum As Integer
    Dim NumberOfAttributes As Integer
    
    ' Start Excel
    On Error Resume Next
    
    Set Excel = GetObject(, "Excel.Application")
    
    If Err <> 0 Then
        Err.Clear
        Set Excel = CreateObject("Excel.Application")
            
        If Err <> 0 Then
            MsgBox "Could not load Excel.", vbExclamation
            End
        End If
    End If
    
   
    
    Excel.Visible = True
    Excel.Workbooks.Add
    Excel.Sheets("Sheet1").Select
    Set excelSheet = Excel.ActiveWorkbook.Sheets("Sheet1")
    
    RowNum = 1
   
   For Each elem In ThisDrawing.ModelSpace
        If StrComp(elem.EntityName, "AcDbPolyline", 1) = 0 Then
            If elem.HasAttributes Then
 ''通过getattributes函数我们把块的属性放入数组中,下图可见数组有3个项目
 ''每个项目都有tagstring和textstring,然后把数组中值输出到excel,至此
 ''我们提取出了块中的全部属性
                elem.Highlight (True)
                elem.GetXData "", xtypeOut, xdataOut
                
                For Count = LBound(xtypeOut) To UBound(xtypeOut)
                   
                        If StrComp(xtypeOut(Count).EntityName, "AcDbAttribute", 1) = 0 Then
                        
                            excelSheet.Cells(RowNum, Count + 1).Value = xtypeOut(Count)
                      
                        End If
                  
                Next Count
                RowNum = RowNum + 1
                For Count = LBound(xdataOut) To UBound(xdataOut)
                    excelSheet.Cells(RowNum, Count + 1).Value = xdataOut(Count)
                Next Count
              
            End If
             RowNum = RowNum + 1
        End If
       
    Next elem
    
    NumberOfAttributes = RowNum - 1
    
    If NumberOfAttributes > 0 Then
        excelSheet.UsedRange.Font.Bold = True
        
        'For a specific set of attribute information this could
        'be set to fit the exact number of columns.
        excelSheet.Columns("A:G").AutoFit

    Else
        MsgBox "未发现扩展数据" & Space(50) & vbCr & _
 "写代码qq:443440204", vbInformation, "版权所有qq:443440204"
        ''Excel.Quit
    End If
MsgBox "OK" & Space(50) & vbCr & _
 "vba代码二次开发qq:443440204", vbInformation, "版权所有qq:443440204"
End Sub



版本2:要素扩展数据字段相同


Sub 导出扩展数据到excel()
    Dim Excel As Object
    Dim elem As Object
    Dim excelSheet As Object
     Dim xdataOut As Variant
    Dim xtypeOut As Variant
    Dim Count, RowNum As Integer
    Dim NumberOfAttributes As Integer
    
    ' Start Excel
    On Error Resume Next
    
    Set Excel = GetObject(, "Excel.Application")
    
    If Err <> 0 Then
        Err.Clear
        Set Excel = CreateObject("Excel.Application")
            
        If Err <> 0 Then
            MsgBox "不能加载Excel.", vbExclamation
            End
        End If
    End If
    

    
    Excel.Visible = True
    Excel.Workbooks.Add
    Excel.Sheets("Sheet1").Select
    Set excelSheet = Excel.ActiveWorkbook.Sheets("Sheet1")
    
    RowNum = 1
    Dim Header As Boolean
    For Each elem In ThisDrawing.ModelSpace
        
            If elem.HasAttributes Then
                 elem.Highlight (True)
                elem.GetXData "", xtypeOut, xdataOut
                For Count = LBound(xtypeOut) To UBound(xtypeOut)
                    If Header = False Then
                        If StrComp(xtypeOut(Count).EntityName, "AcDbAttribute", 1) = 0 Then
                            excelSheet.Cells(RowNum, Count + 1).Value = xtypeOut(Count)
                        End If
                    End If
                Next Count
                RowNum = RowNum + 1
                For Count = LBound(xdataOut) To UBound(xdataOut)
                    excelSheet.Cells(RowNum, Count + 1).Value = xdataOut(Count)
                Next Count
                Header = True
            End If
        
    Next elem
    
    NumberOfAttributes = RowNum - 1
    
    If NumberOfAttributes > 0 Then
        excelSheet.UsedRange.Font.Bold = True
        
        'For a specific set of attribute information this could
        'be set to fit the exact number of columns.
        excelSheet.Columns("A:G").AutoFit

    Else
        MsgBox "未发现扩展数据" & Space(50) & vbCr & _
 "写代码qq:443440204", vbInformation, "版权所有qq:443440204"
        ''Excel.Quit
    End If
MsgBox "OK" & Space(50) & vbCr & _
 "vba代码二次开发qq:443440204", vbInformation, "版权所有qq:443440204"
End Sub



 

相关推荐

  1. CAD VBA 导出cass扩展数据excel

    2023-12-18 05:50:01       45 阅读
  2. phpspreadsheet导出数据和图片excel

    2023-12-18 05:50:01       31 阅读
  3. Springboot导出mysql数据Excel

    2023-12-18 05:50:01       11 阅读
  4. excel数据导入数据库的方法

    2023-12-18 05:50:01       21 阅读
  5. sqlserver导出数据excel导入另一个数据库

    2023-12-18 05:50:01       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-18 05:50:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-18 05:50:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-18 05:50:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-18 05:50:01       18 阅读

热门阅读

  1. Skywalking告警规则示例

    2023-12-18 05:50:01       36 阅读
  2. C#实现一个安全的事件订阅器

    2023-12-18 05:50:01       39 阅读
  3. linux网络----UDP编程

    2023-12-18 05:50:01       35 阅读
  4. 通过GIT将本地项目上传到gitee

    2023-12-18 05:50:01       39 阅读
  5. MySQL_13.InonDB表空间

    2023-12-18 05:50:01       35 阅读
  6. Spring Boot Docker Compose 支持中文文档

    2023-12-18 05:50:01       33 阅读
  7. adb setting设置

    2023-12-18 05:50:01       37 阅读