如何在ArcMap中加入Text和dBASE文件[附代码]
- 产品教程
- 2007-09-15
- 128热度
- 0评论
首先为Text文件或dBASE文件创建一个与之对应的ITable接口对象,然后通过IMap实例获得IStandaloneTable接口对象和IStandaloneTableCollection接口对象,并设置其属性,最后使用IStandaloneTableCollection.AddStandaloneTable方法将Text文件或dBASE文件加入到当前的ArcMap中。
加入Text文件或dBASE文件的区别仅在于创建ITable对象时IWorkspaceFactory的类型不同,加入Text文件时是TextFileWorkspaceFactory类型,加入dBASE文件时是ShapefileWorkspaceFactory类型。
主要用到了IWorkspaceFactory接口,IWorkspace接口,IFeatureWorkspace接口,ITable接口,IStandaloneTable接口和IStandaloneTableCollection接口。
l 程序说明
函数AddTextFile通过文件路径sFilePath和文件名sFileName找到Text文件并为其创建ITable对象
函数AddDBASEFile通过文件路径sFilePath和文件名sFileName找到dBASE文件并为其创建ITable对象
函数Add_Table_TOC将ITable对象pTable加入到当前的ArcMap中。
l 代码
Private Sub AddTextFile(ByVal sFilePath As String, ByVal sFileName As String)
Dim pWorkspaceFactory As IWorkspaceFactory
Dim pWorkspace As IWorkspace
Dim pFeatureWorkspace As IFeatureWorkspace
Dim pTable As ITable
Dim sDir As String
On Error GoTo ErrorHandler:
sDir = Dir(sFilePath & sFileName & ".txt")
If (sDir = "") Then
MsgBox (sFileName & ".txt" & " 文件不存在")
Exit Sub
End If
'Get the ITable from the geodatabase
Set pWorkspaceFactory = New TextFileWorkspaceFactory
Set pWorkspace = pWorkspaceFactory.OpenFromFile(sFilePath, 0)
Set pFeatureWorkspace = pWorkspace
Set pTable = pFeatureWorkspace.OpenTable(sFileName & ".txt")
'Add the table
Add_Table_TOC pTable
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub
Private Sub AddDBASEFile(ByVal sFilePath As String, ByVal sFileName As String)
Dim pWorkspaceFactory As IWorkspaceFactory
Dim pWorkspace As IWorkspace
Dim pFeatureWorkspace As IFeatureWorkspace
Dim pTable As ITable
On Error GoTo ErrorHandler:
'Get the ITable from the geodatabase
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Set pWorkspace = pWorkspaceFactory.OpenFromFile(sFilePath, 0)
Set pFeatureWorkspace = pWorkspace
Set pTable = pFeatureWorkspace.OpenTable(sFileName)
'Add the table
Add_Table_TOC pTable
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub
Private Sub Add_Table_TOC(pTable As ITable)
Dim pDoc As IMxDocument
Dim pMap As IMap
Dim pStandaloneTable As IStandaloneTable
Dim pStandaloneTableC As IStandaloneTableCollection
On Error GoTo ErrorHandler:
Set pDoc = ThisDocument
Set pMap = pDoc.FocusMap
'Create a new standalone table and add it
'to the collection of the focus map
Set pStandaloneTable = New StandaloneTable
Set pStandaloneTable.Table = pTable
Set pStandaloneTableC = pMap
pStandaloneTableC.AddStandaloneTable pStandaloneTable
'Refresh the TOC
pDoc.UpdateContents
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub
Private Sub UIButtonControl1_Click()
Dim pVBProject As VBProject
On Error GoTo ErrorHandler:
Set pVBProject = ThisDocument.VBProject
'Add text file to ArcMap. Dont include .txt extension
AddTextFile pVBProject.FileName & "........" & "data", "Continents"
'Add dBASE file to ArcMap
AddDBASEFile pVBProject.FileName & "........" & "data", "Continents"
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub