在这种情况下,我必须使用vb.net将Excel表或文件输入到我的数据库中,我知道代码和过程,但问题是
由于某些原因,我不想将配置从86位更改为86位,所以现在我应该做什么更好的解决方案,任何一个人都面临这样的解决方案
我的代码是
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim fBrowse As New OpenFileDialog
With fBrowse
.Filter = "Excel files(*.xlsx)|*.xlsx|All files (*.*)|*.*"
.FilterIndex = 1
.Title = "Import data from Excel file"
End With
If fBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fname As String
fname = fBrowse.FileName
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & fname & " '; " & "Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "CurrencyRate")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
MyConnection.Close()
For Each Dr As DataRow In DtSet.Tables(0).Rows
Next
MsgBox("Successfully Saved")
End If但在这段代码中我得到了错误的
Microsoft.ace.oledb.12.0未在本地机器上注册
发布于 2014-09-13 11:03:19
我很久以前就解决了我的问题,但是我看到很多用户访问这个问题,所以我想我应该回答我的问题来帮助别人。
要下载和安装这个库,请按以下链接互操作
下面是我的代码示例以获得进一步的帮助。
Dim table As New DataTable("CurrencyRate")
Dim OFD As New OpenFileDialog
Dim strDestination As String
With OFD
.Filter = "Excel Office|*.xls;*.xlsx"
.FileName = ""
If .ShowDialog() <> Windows.Forms.DialogResult.OK Then
Return False
End If
strDestination = .FileName
End With
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkbook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim xlRange As Microsoft.Office.Interop.Excel.Range
Dim xlCol As Integer
Dim xlRow As Integer
Dim Data(0 To 3) As String
With table
.Clear()
If strDestination <> "" Then
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkbook = xlApp.Workbooks.Open(strDestination)
xlWorkSheet = xlWorkbook.ActiveSheet()
xlRange = xlWorkSheet.UsedRange
If xlRange.Columns.Count > 0 Then
If xlRange.Rows.Count > 0 Then
For xlRow = 2 To xlRange.Rows.Count 'here the xlRow is start from 2 coz in exvel sheet mostly 1st row is the header row
For xlCol = 1 To xlRange.Columns.Count
Data(xlCol - 1) = xlRange.Cells(xlRow, xlCol).text
Next
.LoadDataRow(Data, True)
Next
xlWorkbook.Close()
xlApp.Quit()
KillExcelProcess()
End If
End If
Else
MessageBox.Show("Please Select Excel File", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End With通过这样做,您将让您在DataTable中使用excel数据,然后在您想要保存的位置上,例如将其保存在sql server或存储区域。请让我进一步澄清。
发布于 2014-03-18 10:34:04
我通过安装2007 Office系统驱动程序和Microsoft数据库引擎2010 Redistributable.Even解决了这个问题,我使用32位Office和64位操作系统以及它的工作well.The下载链接http://www.microsoft.com/en-in/download/details.aspx?id=13255和http://www.microsoft.com/en-in/download/confirmation.aspx?id=23734
我使用的连接代码:
OleDbConnection myConnection = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0; " +
"data source='" + path + "';" +
"Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" ");发布于 2019-03-31 02:03:27
我使用"Microsoft.Jet.OLEDB.4.0“来解决这个问题,如下所示。希望这能帮到别人。
Public OledbString32Bit As String = "Provider=Microsoft.ACE.OLEDB.12.0;" ' 32 Bit
Public OledbString64Bit As String = "Provider=Microsoft.Jet.OLEDB.4.0;" ' 64 Bit
For value As Integer = 0 To 1
vCNNstring = OledbString & _
"Data Source= " & vPath & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
ExcelCNN = New System.Data.OleDb.OleDbConnection(vCNNstring)
ExcelCMD = New System.Data.OleDb.OleDbDataAdapter(vSQL, ExcelCNN)
If SheetName = "Sheet2" Then
Dim a As Integer = 0
End If
Try
ExcelCNN.Open()
Exit For
Catch ex As Exception
' If using Default OledbString32Bit not work , change to use OledbString64Bit and save for further call
OledbString = OledbString64Bit
If value = 1 Then
MsgBox("Error in mc_ExcelTableToDataTable using : " & OledbString & ", Error : " & ex.ToString())
End If
End Try
Nexthttps://stackoverflow.com/questions/22473779
复制相似问题