明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 19326|回复: 31

Autocad与excel,txt,mdb文件通讯小结。

    [复制链接]
发表于 2008-7-30 08:51:00 | 显示全部楼层 |阅读模式
1、与excel通讯接口
  1. Function ReturnxlSheet() As Worksheet
  2.     Dim xlApp As Object    ' This Line ,Not set Excel , run Excel
  3.     'Dim xlsheet As Object
  4.    
  5.     ' 发生错误时跳到下一个语句继续执行
  6.     On Error Resume Next
  7.     ' 连接Excel应用程序
  8.     Set xlApp = GetObject(, "Excel.Application")
  9. '   Debug.Print Err.Number
  10.    
  11.     If Err.Number <> 0 Then
  12.         Set xlApp = CreateObject("Excel.Application")
  13.         xlApp.Visible = True
  14.         xlApp.Workbooks.Add
  15.     End If
  16.     ' 返回当前活动的工作表
  17.     Set ReturnxlSheet = xlApp.ActiveSheet
  18. End Function
主程序要点:
  1. sub main()
  2.     Dim xlSheet As Worksheet
  3.     Set xlSheet = ReturnxlSheet
  4. end sub

评分

参与人数 2威望 +1 明经币 +2 金钱 +40 贡献 +5 激情 +5 收起 理由
98syj + 20 很给力!
mccad + 1 + 2 + 20 + 5 + 5 【精华】好文章

查看全部评分

 楼主| 发表于 2008-7-30 08:56:00 | 显示全部楼层
SQL-TXT方法
  1. Function CAdToText(InputFileName)
  2.   Dim LineData As AcadLine, ArcData As AcadArc
  3.   Close #1
  4.   Open InputFileName For Output As #1
  5.   
  6.   Write #1, "m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "m10", "m11", "m12"
  7.   
  8.     Write #1, m1, m2, m3, m4, m5, m6, m7, m8, m9
  9.    
  10.   Next ent
  11.   
  12.   Close #1
  13.   
  14. End Function
  15. Function SQLRecordsetFromTxt(InputFileName As String) As ADODB.Recordset
  16.   Set conn = CreateObject("ADODB.Connection")
  17.   Set rs = CreateObject("adodb.recordset")
  18.   conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};DBQ=d:", "", ""
  19.   rs.Open " " & InputFileName, conn, 1, 3
  20.   Set RecordsetToExcel = rs
  21. End Function
  22. 主程序:
  23. Sub Main()
  24.   abc = "select  "
  25.   abc = abc & " m7,m2,m4,m5,m6 from temp.txt where m1 = 'AcDbText' "
  26.   Set rsText = SQLRecordFromTxt(abc)
  27. End Sub
回复 支持 1 反对 0

使用道具 举报

发表于 2018-3-23 09:24:42 | 显示全部楼层
,厉害   
发表于 2017-11-8 18:01:10 | 显示全部楼层
谢谢楼主分享!
 楼主| 发表于 2008-7-30 09:00:00 | 显示全部楼层
MDB方法
  1. Private Function CreateConnection(AccessDbName As String) As ADODB.Connection
  2.   Dim ConStr As String, Cnn As ADODB.Connection
  3.   
  4.   Set Cnn = New ADODB.Connection
  5.   With Cnn
  6.     .CursorLocation = adUseClient
  7.     .Provider = "Microsoft.Jet.OLEDB.4.0"
  8.     C & AccessDbName
  9.     Cnn.Open ConStr
  10.   End With
  11.   Set CreateConnection = Cnn
  12. End Function
  13. Sub lll()
  14.   Dim Cnn As ADODB.Connection
  15.   Set Cnn = CreateConnection("E:\MyDrawing\MyDrawing\mdb\HG20592.mdb")
  16.   Dim Rst As ADODB.Recordset
  17.   Set Rst = New ADODB.Recordset
  18.   Dim Sql As String
  19.   Sql = " select a.*,b.cl22 from "
  20.   Sql = Sql & "带颈对焊法兰 as a Inner Join 螺柱A as b  "
  21.   Sql = Sql & " on a.法兰规格 = b.法兰规格 "
  22.   Sql = Sql & "where a.法兰规格 = '150-2.5' and b.法兰规格 = '150-2.5'"
  23.   Rst.Open Sql, Cnn
  24.   With Rst.Fields
  25.     For jj = 0 To .Count - 1
  26.       Debug.Print jj, .Item(jj)
  27.     Next jj
  28.   End With
  29. End Sub
  30. Sub llll()
  31.   Dim Cnn As ADODB.Connection
  32.   Set Cnn = CreateConnection("E:\MyDrawing\MyDrawing\mdb\HG20592.mdb")
  33.   Dim Rst As ADODB.Recordset
  34.   Set Rst = New ADODB.Recordset
  35.   Dim Sql As String
  36.   Sql = " select a.*,b.Bl611 from "
  37.   'Sql = " select a.*,b.* from "
  38.   Sql = Sql & "板式平焊法兰 as a Inner Join 螺栓B as b  "
  39.   Sql = Sql & " on a.法兰规格 = b.法兰规格 "
  40.   Sql = Sql & "where a.法兰规格 = '65-1.6' and b.法兰规格 = '65-1.6'"
  41.   Rst.Open Sql, Cnn
  42.   With Rst.Fields
  43.     For jj = 0 To .Count - 1
  44.       Debug.Print jj, .Item(jj)
  45.     Next jj
  46.   End With
  47. End Sub
 楼主| 发表于 2008-7-30 09:10:00 | 显示全部楼层
本帖最后由 作者 于 2008-7-30 16:34:52 编辑

SQL-EXCEL
  1. Function InCadGetSqlExcelRecordset(Sql As String, InputFileName) As ADODB.Recordset
  2.   
  3.   Dim Rst As New ADODB.Recordset
  4.   Set Cnn = New ADODB.Connection
  5.   Cnn.Open "Provider = MicroSoft.Jet.OLEDB.4.0; Extended Properties = Excel 8.0; Data Source = " & InputFileName
  6.    
  7.   Rst.Open Sql, Cnn, adOpenStatic
  8.   Set InCadGetSqlExcelRecordset = Rst
  9. End Function
  10. Function FromSheetReturnxlSheet(SheetName As String) As Worksheet
  11.     Dim xlApp As Application   'As Object    ' This Line ,Not set Excel , run Excel
  12.     'Dim xlsheet As Object
  13.    
  14.     ' 发生错误时跳到下一个语句继续执行
  15.     On Error Resume Next
  16.     ' 连接Excel应用程序
  17.     Set xlApp = GetObject(, "Excel.Application")
  18. '   Debug.Print Err.Number
  19.    
  20.     If Err.Number <> 0 Then
  21.         Set xlApp = CreateObject("Excel.Application")
  22.         xlApp.Visible = True
  23.         xlApp.Workbooks.Add
  24.     End If
  25.     ' 返回当前活动的工作表
  26.     'xlApp.ActiveWorkbook.Sheets (SheetName)
  27.     Set FromSheetReturnxlSheet = xlApp.ActiveWorkbook.Sheets(SheetName)
  28. End Function
  29. Sub ll()
  30.   Dim Rst As ADODB.Recordset
  31.   Dim Sql As String
  32.   Sql = "Select distinct m1 from [Sheet1$]  "
  33.   Set Rst = InCadGetSqlExcelRecordset(Sql, "d:\ls.xls")
  34.   Dim xlSheet  As Worksheet
  35.   Set xlSheet = FromSheetReturnxlSheet("Sheet3")
  36.   xlSheet.Range("a:z").ClearContents
  37.   xlSheet.Cells(1, 1).CopyFromRecordset Rst
  38. End Sub
发表于 2008-7-30 11:58:00 | 显示全部楼层
顶版主
发表于 2008-8-6 18:10:00 | 显示全部楼层

支持版主,希望以后能多向你请教,呵呵!

发表于 2008-8-17 22:05:00 | 显示全部楼层
谢谢你,版主!!!
发表于 2008-8-30 09:47:00 | 显示全部楼层

好东东

发表于 2008-9-5 09:49:00 | 显示全部楼层

不错不错,内容好极了,我就不用一个一个的找了

发表于 2008-9-5 09:49:00 | 显示全部楼层
不错的东西
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-25 09:40 , Processed in 0.169112 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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