Hello, I got this error, what is wrong ?
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Public Module AddExcelFile
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Public Module AddExcelFile
Public Function OpenExcelFile() As FeatureSet
Dim openDialog As New OpenFileDialog()
openDialog.Filter = "Excel Files|*.xlsx"
If openDialog.ShowDialog() = DialogResult.OK Then
Dim excelTable As DataTable = ConvertExcelFileToDataTable(openDialog.FileName)
Return ConvertDataTableToFeatureSet(excelTable)
End If
Return Nothing
End Function
Private Function ConvertExcelFileToDataTable(excelFileName As String) As DataTable
Dim connectionString As String = [String].Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1""", excelFileName)
Using connection As New OleDbConnection(connectionString)
Dim query As String = "SELECT * FROM [Sheet1$]"
connection.Open()
Dim command As New OleDbCommand(query, connection)
Dim adapter As New OleDbDataAdapter(command)
Dim excelTable As New DataTable()
adapter.Fill(excelTable)
Return excelTable
End Using
End Function
Private Function ConvertDataTableToFeatureSet(excelTable As DataTable) As FeatureSet
' See if table has the lat, long columns
If excelTable.Columns.Contains("x") And excelTable.Columns.Contains("y") Then
Dim fs As New FeatureSet(FeatureType.Point)
fs.Projection = KnownCoordinateSystems.Geographic.World.WGS1984
' Set columns of attribute table
fs.DataTable = excelTable.Clone()
For Each excelRow As DataRow In excelTable.Rows
Dim lat As Double = [Double].Parse(excelRow("x").ToString())
Dim lon As Double = [Double].Parse(excelRow("y").ToString())
' Add the point to the FeatureSet
Dim coord As New Coordinate(lon, lat)
Dim point As New Point(coord)
Dim feature As IFeature = fs.AddFeature(point)
' Bring over all of the data as attribute data.
For i As Integer = 0 To excelTable.Columns.Count - 1
feature.DataRow(i) = excelRow(i)
Next
Next
Return fs
Else
MessageBox.Show("The excel table must have lat and long columns.")
Return Nothing
End If
End Function
End Module