使用 VB.Net 应用程序将 Excel 数据导入 SQL 服务器 table
Import Excel data to SQL Server table using VB.Net application
我找到了几篇关于这个主题的文章,但问题各不相同。我正在尝试使用 VB.net 应用程序将 Excel 2016 文件中的数据导入 SQL Server 2017,以便最终用户无需安装 SQL 服务器在他们的机器上。我 运行 应用程序处于调试模式以识别问题。
我再次阅读了有关与以下错误相关的各种实例的文章:
The 'Microsoft.ACE.OLEDB.15.0' provider is not registered on the local machine
我的代码:
Dim TheData As DataTable = GetExcelData(OpenFileDialog1)
Private Function GetExcelData(File As OpenFileDialog) As DataTable
Dim oleDbConnection As New OleDbConnection
Try
OpenOLEDBConnections(oleDbConnection, "Provider=Microsoft.ACE.OLEDB.15.0;Data Source=" & File.InitialDirectory & _
File.FileName & "; ;Extended Properties=""Excel 12.0 XML;HDR=YES;""")
Dim oleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter("Select '' as ID, 'AL' as Supplier, LTRIM(RTRIM(GRADE)) AS GRADE, [ship date] as shipdate, LTRIM(RTRIM(coil)) AS coil, L15*1000 AS L15, H15*1000 AS H15 FROM [sheet1$] where COIL is not null group by GRADE, [SHIP DATE], COIL, L15, H15", oleDbConnection)
Dim dataTable As DataTable = New DataTable()
oleDbDataAdapter.Fill(dataTable)
oleDbConnection.Close()
Return dataTable
Catch ex As Exception
oleDbConnection.Close()
Return Nothing
End Try
End Function
Private Sub OpenOLEDBConnections(ByRef cnData As OleDbConnection, cnDataConstr As String)
If Not cnData.State = ConnectionState.Closed Then cnData.Close()
If cnData.State = ConnectionState.Closed Then
cnData.ConnectionString = cnDataConstr
cnData.Open()
End If
End Sub
并不是说这是灵丹妙药,但我不喜欢您的连接字符串的外观。似乎有一个“;”空间不足。试试这个
Dim XLSFile As String = "C:\DATA\test.xlsx"
Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=" & XLSFile & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
尝试从以下 link
安装 ACE 提供程序
https://www.microsoft.com/en-us/download/confirmation.aspx?id=13255
如何将 Excel 导入 SQL 服务器,查看
https://www.red-gate.com/simple-talk/dotnet/c-programming/office-development-in-visual-studio/
或尝试使用此代码段
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Public Module Program
Public Sub Main()
Dim officeType = Type.GetTypeFromProgID("Excel.Application")
If officeType Is Nothing Then
Console.WriteLine("Sorry, Excel must be installed!")
Console.WriteLine("Press any key to exit")
Console.ReadLine()
Return
End If
Const fileToRead As String = "C:\TMP\FirstTest.xlsx"
Dim xlApp As Excel.Application = Nothing
Dim workbooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim sheets As Excel.Sheets = Nothing
Dim t1 As Excel.Worksheet = Nothing
Try
xlApp = New Excel.Application()
Console.WriteLine($"Trying to open file {fileToRead}")
workbooks = xlApp.Workbooks
xlWorkBook = workbooks.Open(fileToRead, 0, True, 5, "", "", True, Origin:=Excel.XlPlatform.xlWindows, Delimiter:=vbTab, Editable:=False, Notify:=False, Converter:=0, AddToMru:=True, Local:=1, CorruptLoad:=0)
sheets = xlApp.ActiveWorkbook.Sheets
Dim dic = New List(Of String)()
For Each mSheet In sheets
dic.Add($"[{t1.Name}$]")
Next
Using myConnection = New OleDbConnection($"Provider=Microsoft.Ace.OLEDB.12.0;Data Source={fileToRead};Extended Properties='Excel 12.0 Xml;HDR = YES;'")
Using dtSet = New DataSet()
For Each s In dic
Console.WriteLine($" Processing {s} table")
Dim myCommand = New OleDbDataAdapter($"select * from {s};", myConnection)
myCommand.TableMappings.Add("Table", s)
myCommand.Fill(dtSet)
Next
For Each t As DataTable In dtSet.Tables
Console.WriteLine($" Table {t.TableName} has {t.Rows.Count} records")
Next
End Using
End Using
dic = Nothing
Console.WriteLine("Successfully imported!")
Console.WriteLine("After closing Console Windows start Task Manager and be sure that Excel instance is not there!")
Console.WriteLine("Press any key to exit")
Console.ReadLine()
Catch e As Exception
Console.WriteLine($"Error importing from Excel : {e.Message}")
Console.ReadLine()
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
If sheets IsNot Nothing Then
Marshal.FinalReleaseComObject(sheets)
sheets = Nothing
End If
xlWorkBook.Close()
If xlWorkBook IsNot Nothing Then
Marshal.FinalReleaseComObject(xlWorkBook)
xlWorkBook = Nothing
End If
xlApp.Quit()
If xlApp IsNot Nothing Then
Marshal.FinalReleaseComObject(xlApp)
xlApp = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
End Module
在我花了一天半的时间后,我设法通过以下 link 解决了问题,并尝试了 Manju K Gowda 2012 年 5 月 14 日星期一提到的选项12:14下午
Microsoft.ACE.OLEDB.15.0 并将我的连接字符串提供程序从 OLEDB 15.0 修改为 12.0。
准确地说,在调试或发布模式下构建解决方案时,将配置管理器保持为 x86 而不是 AnyCPU
我找到了几篇关于这个主题的文章,但问题各不相同。我正在尝试使用 VB.net 应用程序将 Excel 2016 文件中的数据导入 SQL Server 2017,以便最终用户无需安装 SQL 服务器在他们的机器上。我 运行 应用程序处于调试模式以识别问题。
我再次阅读了有关与以下错误相关的各种实例的文章:
The 'Microsoft.ACE.OLEDB.15.0' provider is not registered on the local machine
我的代码:
Dim TheData As DataTable = GetExcelData(OpenFileDialog1)
Private Function GetExcelData(File As OpenFileDialog) As DataTable
Dim oleDbConnection As New OleDbConnection
Try
OpenOLEDBConnections(oleDbConnection, "Provider=Microsoft.ACE.OLEDB.15.0;Data Source=" & File.InitialDirectory & _
File.FileName & "; ;Extended Properties=""Excel 12.0 XML;HDR=YES;""")
Dim oleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter("Select '' as ID, 'AL' as Supplier, LTRIM(RTRIM(GRADE)) AS GRADE, [ship date] as shipdate, LTRIM(RTRIM(coil)) AS coil, L15*1000 AS L15, H15*1000 AS H15 FROM [sheet1$] where COIL is not null group by GRADE, [SHIP DATE], COIL, L15, H15", oleDbConnection)
Dim dataTable As DataTable = New DataTable()
oleDbDataAdapter.Fill(dataTable)
oleDbConnection.Close()
Return dataTable
Catch ex As Exception
oleDbConnection.Close()
Return Nothing
End Try
End Function
Private Sub OpenOLEDBConnections(ByRef cnData As OleDbConnection, cnDataConstr As String)
If Not cnData.State = ConnectionState.Closed Then cnData.Close()
If cnData.State = ConnectionState.Closed Then
cnData.ConnectionString = cnDataConstr
cnData.Open()
End If
End Sub
并不是说这是灵丹妙药,但我不喜欢您的连接字符串的外观。似乎有一个“;”空间不足。试试这个
Dim XLSFile As String = "C:\DATA\test.xlsx"
Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=" & XLSFile & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
尝试从以下 link
安装 ACE 提供程序https://www.microsoft.com/en-us/download/confirmation.aspx?id=13255
如何将 Excel 导入 SQL 服务器,查看
https://www.red-gate.com/simple-talk/dotnet/c-programming/office-development-in-visual-studio/
或尝试使用此代码段
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Public Module Program
Public Sub Main()
Dim officeType = Type.GetTypeFromProgID("Excel.Application")
If officeType Is Nothing Then
Console.WriteLine("Sorry, Excel must be installed!")
Console.WriteLine("Press any key to exit")
Console.ReadLine()
Return
End If
Const fileToRead As String = "C:\TMP\FirstTest.xlsx"
Dim xlApp As Excel.Application = Nothing
Dim workbooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim sheets As Excel.Sheets = Nothing
Dim t1 As Excel.Worksheet = Nothing
Try
xlApp = New Excel.Application()
Console.WriteLine($"Trying to open file {fileToRead}")
workbooks = xlApp.Workbooks
xlWorkBook = workbooks.Open(fileToRead, 0, True, 5, "", "", True, Origin:=Excel.XlPlatform.xlWindows, Delimiter:=vbTab, Editable:=False, Notify:=False, Converter:=0, AddToMru:=True, Local:=1, CorruptLoad:=0)
sheets = xlApp.ActiveWorkbook.Sheets
Dim dic = New List(Of String)()
For Each mSheet In sheets
dic.Add($"[{t1.Name}$]")
Next
Using myConnection = New OleDbConnection($"Provider=Microsoft.Ace.OLEDB.12.0;Data Source={fileToRead};Extended Properties='Excel 12.0 Xml;HDR = YES;'")
Using dtSet = New DataSet()
For Each s In dic
Console.WriteLine($" Processing {s} table")
Dim myCommand = New OleDbDataAdapter($"select * from {s};", myConnection)
myCommand.TableMappings.Add("Table", s)
myCommand.Fill(dtSet)
Next
For Each t As DataTable In dtSet.Tables
Console.WriteLine($" Table {t.TableName} has {t.Rows.Count} records")
Next
End Using
End Using
dic = Nothing
Console.WriteLine("Successfully imported!")
Console.WriteLine("After closing Console Windows start Task Manager and be sure that Excel instance is not there!")
Console.WriteLine("Press any key to exit")
Console.ReadLine()
Catch e As Exception
Console.WriteLine($"Error importing from Excel : {e.Message}")
Console.ReadLine()
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
If sheets IsNot Nothing Then
Marshal.FinalReleaseComObject(sheets)
sheets = Nothing
End If
xlWorkBook.Close()
If xlWorkBook IsNot Nothing Then
Marshal.FinalReleaseComObject(xlWorkBook)
xlWorkBook = Nothing
End If
xlApp.Quit()
If xlApp IsNot Nothing Then
Marshal.FinalReleaseComObject(xlApp)
xlApp = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
End Module
在我花了一天半的时间后,我设法通过以下 link 解决了问题,并尝试了 Manju K Gowda 2012 年 5 月 14 日星期一提到的选项12:14下午 Microsoft.ACE.OLEDB.15.0 并将我的连接字符串提供程序从 OLEDB 15.0 修改为 12.0。
准确地说,在调试或发布模式下构建解决方案时,将配置管理器保持为 x86 而不是 AnyCPU