Import Data From MS Excel to DataSet without using COM Objects

In my last blog I demonstrated how to export data from a DataSet/DataTable to MS Excel file. In this blog I will show you the process reversed - import data from excel to a Dataset without using the COM objects.

Now let's start with an code example.

Code Example

Public Class DataImporter

Public Shared Function ImportExcelToDataSet(ByVal excelFullFileName As String, ByVal targetTable As String) As DataSet
Dim oledbConnection As OleDbConnection = GetOledbConnection(excelFullFileName)

Dim commandText As String = "select * from [Sheet1$]"

Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter(commandText, oledbConnection)

Dim ds As DataSet = New DataSet()

Try
oledbConnection.Open()
dataAdapter.TableMappings.Add("Table", targetTable)
dataAdapter.Fill(ds)

Catch ex As Exception
Throw
Finally
If oledbConnection.State = ConnectionState.Open Then
oledbConnection.Close()
End If

End Try

Return ds

End Function

Private Shared Function GetFileExtension(ByVal fullFileName As String) As String
Return fullFileName.Substring(fullFileName.LastIndexOf(".") + 1)
End Function

Private Shared Function GetOledbConnection(ByVal excelFullFileName As String) As OleDbConnection
Dim excelConnectionString As String
Dim excelExtension As String = GetFileExtension(excelFullFileName)

'' default for excel 97-2003
'excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
' "Data Source=" & excelFullFileName & ";" & _
' "Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;"""

'' for excel 2007 and above
'If excelExtension.ToLower.Equals("xlsx") Then
' excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
' "Data Source=" & excelFullFileName & ";" & _
' "Extended Properties=""'Excel 12.0;HDR=YES;IMEX=1;"""
'End If

' both excel 2003 and 2007 works right under this setting
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & excelFullFileName & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""

Return New OleDbConnection(excelConnectionString)
End Function

End Class

Important Notes

1. This code example works fine for both Excel 2007 and Excel 97~2003.

2. In order to run this code example, you must install 2007 Office System Driver: Data Connectivity Components ("AccessDatabaseEngine.exe") on your target machine. You can download it from the following url.

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

3. This code examplse has tested on Windows2003, Windows 7, Excel 2003 and Excel 2007.

References

http://www.codeproject.com/KB/database/Excel2Sql.aspx

http://social.msdn.microsoft.com/Forums/en/vsto/thread/43d6b544-7172-4fe7-babf-a541e5f6a1c9

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13651903/viewspace-1043350/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/13651903/viewspace-1043350/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值