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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- How To Replicate Data from Oracle to Postgres Using GoldenGate (Doc ID 1544137.1OracleGo
- 2.1.3.3 Container Data Objects in a CDBAIObject
- from bson import ObjectIdImportObject
- from selenium import webdriverImportWeb
- python from import 出錯PythonImport
- 翻譯 | Learning React Without Using React Part 2React
- 翻譯 | Learning React Without Using React Part 1React
- 2.3.2.3 Data-Linked Application Common ObjectsAPPObject
- Python import相關內容區別介紹( import *** as 、from***import )PythonImport
- 徹底搞懂Python 中的 import 與 from importPythonImport
- Export/import Datas To/from a Csv FileExportImport
- MGTSC 212 using ExcelExcel
- python基礎--自定義模組、import、from......import......PythonImport
- 當執行import vue from ‘vue‘ 或其它import xxx from ‘xxx‘ 時發生了什麼?ImportVue
- get_data_from_yml
- Converting Oracle Database from Linux to Windows using RMANOracleDatabaseLinuxWindows
- cannot import name ‘multiarray‘ from ‘numpy.core‘Import
- Step by Step Data Replication Using Oracle GoldenGateOracleGo
- PostgreSQL DBA(181) - Using PostgreSQL as a Data WarehouseSQL
- ImportError: cannot import name ‘BaseQuery‘ from ‘flask_sqlalchemy‘ImportErrorFlaskSQL
- 【譯】Using .NET for Apache Spark to Analyze Log DataApacheSpark
- [Javascript] Find Items from the end of the JavaScript Array using at, findLast and findLastIndexJavaScriptASTIndex
- SciTech-Mathmatics-ImageProcessing-Remove the Background from an image using Python?REMPython
- OAF export data from VO in xlsx formatExportORM
- Connect SQL Server from Linux Client using Windows Authentication and troubleshoot stepsSQLServerLinuxclientWindows
- 解決 ImportError: cannot import name 'imread' from 'scipy.misc'ImportError
- 【等待事件】SQL*Net more data from dblink事件SQL
- Nginx編譯時error: assignment makes pointer from integer without a cast處理Nginx編譯ErrorAST
- ImportError: cannot import name 'get_ora_doc' from partially initialized moduleImportErrorZed
- PHP設計模式-DAO (Data Access Objects) 資料訪問物件模式PHP設計模式Object物件
- ObjectsObject
- 論文解讀(SimGRACE)《SimGRACE: A Simple Framework for Graph Contrastive Learning without Data Augmentation》FrameworkAST
- python3 筆記17.呼叫模組from...import...Python筆記Import
- win10 vscode 快速修復 沒有Update import from "Vue"Win10VSCodeImportVue
- 【PTD】DEM generation from laser scanner data using adaptive TIN models利用自適應TIN模型從鐳射掃描器資料生成DEMAPT模型
- tf.data.Dataset.shuffle(buffer_size)中buffer_size的理解
- 加速訓練之並行化 tf.data.Dataset 生成器並行
- MySQL主從複製報錯:Got fatal error 1236 from master when reading data fromMySqlGoErrorAST
- git@github.com: Permission denied (publickey). fatal: Could not read from remoteGithubREM