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.
3. This code examplse has tested on Windows2003, Windows 7, Excel 2003 and Excel 2007.
References
[@more@]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1043350/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Export a DataSet to Microsoft Excel without using the COM objectsExportROSExcelObject
- Get data style from ExcelExcel
- 2.3.2.3 Data-Linked Application Common ObjectsAPPObject
- Case two -- Recover a datafile in primary site from the backup taken in standby site without using c
- Import DataImport
- from module import 和 import 的區別Import
- Step by Step Guide on Creating Physical Standby Using RMAN DUPLICATE...FROM ACTIVE DATABASE WithoutGUIIDEDatabase
- PDO--PHP Data ObjectsPHPObject
- Operating on Specific Objects Using EXPDPObject
- import tkinter與from tkinter import *的區別Import
- from selenium import webdriverImportWeb
- from bson import ObjectIdImportObject
- Export data from a data blockExportBloC
- How To Replicate Data from Oracle to Postgres Using GoldenGate (Doc ID 1544137.1OracleGo
- Using Morphia to map Java objects in MongoDBJavaObjectMongoDB
- 2.1.3.3 Container Data Objects in a CDBAIObject
- Convert Array of Objects to Data TableObject
- Export/import Datas To/from a Csv FileExportImport
- python from import 出錯PythonImport
- from ... import ... 執行機制Import
- Python import相關內容區別介紹( import *** as 、from***import )PythonImport
- python基礎--自定義模組、import、from......import......PythonImport
- 徹底搞懂Python 中的 import 與 from importPythonImport
- Data Pump with Network importImport
- from Delphi to ExcelExcel
- How to open MS word document from the SharePoint 2010 using Microsoft.Office.Interop.dllROS
- rdo(remote data objects) repo openstack icehouseREMObject
- Extracting Data from a Corrupt Table using DBMS_REPAIR or Event 10231AI
- the method of fixing the problem of using SSIS and SharePointListAdapters to sync data from DB to ShAPT
- Data Utilities : Export and Import Utilities (57)ExportImport
- Introduce to Data Storage Objects in ASP.NETObjectASP.NET
- 2.3.2 Application Common ObjectsAPPObject
- cannot import name ‘multiarray‘ from ‘numpy.core‘Import
- Regression Analysis Using ExcelExcel
- Unload data from oracleOracle
- get_data_from_yml
- 翻譯 | Learning React Without Using React Part 2React
- 翻譯 | Learning React Without Using React Part 1React