我有一个Access 2013数据库,所有表都链接到SQL Server 2016表。我有一个Excel 2013(.xlsx)文件,我需要将其导入到Ms Access中的表中,该表通过vba代码链接到SQL Server(xlsx和表中的所有字段都相同)
我所有的VBA代码都驻留在Access数据库中,我有一个带有事件按钮的表单,我尝试对SQL使用de“ transferspreadsheet”,“ Insert to”子句,但是它们都不对我有用
这是我的代码,
xtRuta2表单中具有路径的字段的名称 Dim strArchivo2字符串'文件xlsx的路径c:\ reports \ mireporte.xlsx 将miAlerta2暗淡为字符串 昏暗的ssql作为字符串
strArchivo2 = txtRuta2
miAlerta2 = MsgBox(“¿您是否要为”&strArchivo2&“?&vbCrLf&vbCrLf&”此操作将更新所有信息进行导入“,vbExclamation + vbOKCancel,”¡信息导入警告! “)
如果miAlerta2 = vbOK,则 varAlert2 = MsgBox(“请确认您要导入新信息吗?”,vbExclamation + vbOKCancel,“¡确认输入警告!”) 如果varAlert2 = vbOK然后
'DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tbl_ZSales_Export Worksheet", strArchivo2, True, "Export Worksheet$"
ssql = "INSERT INTO [tbl_Export Worksheet] select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=" & strArchivo2 & ";HDR=YES', 'SELECT * FROM [Export Worksheet$)'"
'CurrentDb.Execute ssql
MsgBox "Import Finished", vbExclamation + vbOKOnly
endif 如果结束
您能帮我写出正确的代码吗?
谢谢!
答案 0 :(得分:0)
这段代码(与excel进行后期bdingin交互)用于将excel工作表转换为文本文件,然后导入到您选择的表中。我更喜欢使用这种方法,因为访问有一个烦人的习惯,即在使用transferspreadsheet时尝试为您解释数据。通过创建导入规范(使用此方法需要执行此操作),可以轻松地预定义数据类型。
Option Compare Database
Option Explicit
Private Sub stuff()
On Error GoTo GetAccrualFile_Err
Dim fileLoc As String
Dim path As String, Sep As String, NewTextFile As String, WholeLine As String
Dim oXL As Object, sheet As Object
Dim i As Long, j As Long, counteri As Long, counterj As Long
Dim bringOver As Variant
DoCmd.SetWarnings False
DoCmd.Hourglass True
counteri = 0
counterj = 0
Sep ="your prefered delimiter"
DoCmd.RunSQL "DELETE * FROM TBL"
fileLoc = "UNC PATH AND FILE NAME" & ".xlsx"
path = Left(fileLoc, InStrRev(fileLoc, "\") - 1) & "\"
NewTextFile = "UNC PATH AND FILE NAME" & ".txt"
Set oXL = CreateObject("Excel.Application")
With oXL
.WorkBooks.Open FileName:=path & Dir$(fileLoc)
Open NewTextFile For Output As #2
bringOver = .Worksheets("your sheet name").UsedRange 'you might need to adjust this line to get the sheet your after
For i = LBound(bringOver, 1) To UBound(bringOver, 1)
For j = LBound(bringOver, 2) To UBound(bringOver, 2)
WholeLine = WholeLine & bringOver(i, j) & Sep
counterj = counterj + 1
Next j
'used if you want to skip column headers
If counteri <> 0 Then
Print #2, WholeLine
End If
WholeLine = ""
counteri = counteri + 1
counterj = 0
Next i
counteri = 0
Erase bringOver
End With
Close #2
DoCmd.TransferText acImportDelim, "importspecname", "tbltoimportto", NewTextFile, False
'***************************************************************************************
'you will need to learn how to set up import specs, as well as understand the arguments for DoCmd.TransferText
'***************************************************************************************
CleanUp:
DoCmd.SetWarnings True
DoCmd.Hourglass False
On Error Resume Next
DoEvents
oXL.Quit
oXL.Application.Quit
If Dir(NewTextFile) <> "" Then Kill NewTextFile
Erase bringOver
DoCmd.SetWarnings True
DoCmd.Hourglass False
Exit Sub
GetAccrualFile_Err:
DoCmd.SetWarnings True
DoCmd.Hourglass False
msgbox "An error has occured. " & " " & ERR.Number & " " & ERR.Description & " "
GoTo CleanUp
Resume
End Sub
答案 1 :(得分:-1)
尝试使用EPPlus,这是一个免费的库,它使您可以从.Net平台管理Excel文件。 这里有一个教程:https://riptutorial.com/epplus