Importing Excel into a DataTable Quickly

前端 未结 6 1941
-上瘾入骨i
-上瘾入骨i 2020-11-29 02:49

I am trying to read an Excel file into a list of Data.DataTable, although with my current method it can take a very long time. I essentually go Worksheet by Worksheet, cell

6条回答
  •  悲&欢浪女
    2020-11-29 03:29

    Dim sSheetName As String
    Dim sConnection As String
    Dim dtTablesList As DataTable
    Dim oleExcelCommand As OleDbCommand
    Dim oleExcelReader As OleDbDataReader
    Dim oleExcelConnection As OleDbConnection
    
    sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Test.xls;Extended Properties=""Excel 12.0;HDR=No;IMEX=1"""
    
    oleExcelConnection = New OleDbConnection(sConnection)
    oleExcelConnection.Open()
    
    dtTablesList = oleExcelConnection.GetSchema("Tables")
    
    If dtTablesList.Rows.Count > 0 Then
        sSheetName = dtTablesList.Rows(0)("TABLE_NAME").ToString
    End If
    
    dtTablesList.Clear()
    dtTablesList.Dispose()
    
    If sSheetName <> "" Then
    
        oleExcelCommand = oleExcelConnection.CreateCommand()
        oleExcelCommand.CommandText = "Select * From [" & sSheetName & "]"
        oleExcelCommand.CommandType = CommandType.Text
    
        oleExcelReader = oleExcelCommand.ExecuteReader
    
        nOutputRow = 0
    
        While oleExcelReader.Read
    
        End While
    
        oleExcelReader.Close()
    
    End If
    
    oleExcelConnection.Close()
    

提交回复
热议问题