Ok, I whipped up the other portion real quick, so beware that while it seemed to work ok for me, it may not take into account all of the little things. Basically, this is just to get you going and you really should extensively test for your implementation.It's fairly simple, i just did all of this in one Class. Just start it off with the click event of a button. Here's the rest of the code you'll needGood luck!!!Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pLayer As IFeatureLayer
pLayer = GetLayer("Add your layer name here")
If pLayer Is Nothing Then
MsgBox("Layer Not Found... Exiting")
Exit Sub
End If
buildTempDataTable(pLayer)
End Sub
You'll need to figure out how to get the m_pApp ref!Private Function GetLayer(ByVal lyrName As String) As IFeatureLayer
Dim pDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
Dim pMap As ESRI.ArcGIS.Carto.IMap
Dim pLayer As ESRI.ArcGIS.Carto.IFeatureLayer
Dim i As Short
'Layer name to find
Dim sLayerName As String
sLayerName = lyrName
pDoc = m_pApp.Document
pMap = pDoc.FocusMap
For i = 0 To pMap.LayerCount - 1
If pMap.Layer(i).Name = sLayerName Then
pLayer = pMap.Layer(i)
Exit For
End If
Next
Return pLayer
End Function
'this builds your ADO.NET DataTable. But may not add all required field types!!!Private Sub buildTempDataTable(ByVal pLayer As IFeatureLayer)
Dim tmpDT As New DataTable("tmpDT")
Dim column As DataColumn
Dim pTable As ITable = pLayer.FeatureClass
Dim pFields As IFields = pTable.Fields
Dim pCur As ICursor = pTable.Search(Nothing, False)
For c = 0 To pCur.Fields.FieldCount - 1
column = New DataColumn()
column.ColumnName = (pFields.Field(c).Name)
If pFields.Field(c).Type = esriFieldType.esriFieldTypeString Then
column.DataType = System.Type.GetType("System.String")
ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeInteger Then
column.DataType = System.Type.GetType("System.Int32")
ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeDouble Then
column.DataType = System.Type.GetType("System.Double")
ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeDate Then
column.DataType = System.Type.GetType("System.DateTime")
End If
column.ReadOnly = False
tmpDT.Columns.Add(column)
Next
Dim pRow As IRow = pCur.NextRow
Dim newRow As DataRow
Do Until pRow Is Nothing
newRow = tmpDT.NewRow()
newRow.BeginEdit()
For cols = 0 To pCur.Fields.FieldCount - 1
newRow(cols) = pRow.Value(pRow.Fields.FindField(pFields.Field(cols).Name))
Next
newRow.EndEdit()
tmpDT.Rows.Add(newRow)
tmpDT.AcceptChanges()
pRow = pCur.NextRow
Loop
PopulateExcelTable(tmpDT)
End Sub
'This populates the Excel tablePrivate Sub PopulateExcelTable(ByVal ADO_netDataTable As DataTable)
Try
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("c:\test.xls") 'specify your .xls
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
For i = 0 To ADO_netDataTable.Rows.Count - 2
For j = 0 To ADO_netDataTable.Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = ADO_netDataTable.Rows(i)(j).ToString
Next
Next
xlWorkSheet.SaveAs("C:\test.xls") 'specify your .xls
xlWorkBook.Close()
xlApp.Quit()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub