我正在尝试将 Excel 文件 Excel97-2003(仅限第一张工作表)导入 Access 数据库。为此,我正在使用以下代码:
Dim cnnExcel As New ADODB.Connection
Dim rsExcel As New ADODB.Recordset
With cnnExcel
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & strFileSelected & ";" & "Extended Properties=Excel 8.0"
.CursorLocation = adUseClient
.Open
End With
rsExcel.Open "SELECT * FROM [Sheet1]", cnnExcel
excel文件中的所有列都是字符串,当你用Excel2003打开它时,类型为“General”(没有特殊格式),其他3列是日期。
我的问题是单元格中存在的值为空。例如,如果第一个值是字符串,而不是该列中有整数,则它将返回为空,反之亦然
有什么解决办法吗?
谢谢
最佳答案
解决方案一
使用您的代码,我能够毫无问题地从 Excel 工作表中正确加载数据。
但是,请检查您的 SQL 查询,它应该类似于:
rsExcel.Open "SELECT * FROM [Sheet 1$]", cnnExcel
FROM
的规则部分如下:SELECT * FROM [SheetName$]
,请注意 $
SELECT * FROM [SheetName$A1:C5]
SELECT * FROM NameRange
SELECT * FROM ['This;is.My SheetName$']
在我的机器上工作的代码:
Dim cnnExcel As Object
Dim rsExcel As Object
Set cnnExcel = CreateObject("ADODB.Connection")
Set rsExcel = CreateObject("ADODB.RecordSet")
With cnnExcel
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=D:\Query1.xls;Extended Properties=Excel 8.0"
.CursorLocation = 3
.Open
End With
rsExcel.Open "SELECT * FROM [Sheet 1$]", cnnExcel
Debug.Print rsExcel.RecordCount ' Prints the number of rows containing data '
Do
Debug.Print "Col1: " & rsExcel.Fields(0) & " - Col2: " & rsExcel.Fields(1)
rsExcel.MoveNext
Loop While Not rsExcel.EOF
rsExcel.Close
解决方案二
直接操作 Excel 工作簿可能会更好。
假设您有一张表
MyTable
在要导入的 Access 数据库中的 myA
字段中, myB
和 myC
(具有您期望的正确数据类型!) Excel 的内容 Sheet 1
有相应的列。简化的 VBA 代码如下所示:
Sub ImportData(fname As String)
Dim xlo As Object
Dim xlWb As Object
Dim xlWs As Object
Dim colA, colB, ColC As Variant
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim thereIsData As Boolean
Dim row As Integer
' Open Excel sheet, try to re-use Excel if it is open '
On Error Resume Next
Set xlo = GetObject("Excel.Application")
If xlo Is Nothing Then Set xlo = CreateObject("Excel.Application")
On Error Goto 0
Set xlWb = xla.Workbooks.Open(fname)
Set xlWs = xlWb.Worksheets(1) ' Sheet 1'
' Open table where the results will be stored '
Set db = CurrentDb()
Set rs = db.OpenRecordset("MyTable", dbOpenDynaset)
Do
colA = xlWs.Cells(row, 1).Value
colB = xlWs.Cells(row, 2).Value
colC = xlWs.Cells(row, 3).Value
' We will stop at the first empty row '
thereIsData = Not (IsBlank(colA) And IsBlank(colB) And IsBlank(colC))
If thereIsData Then
' Add the Excel data to the table '
rs.AddNew
rs!myA = colA
rs!myA = colB
rs!myA = colC
rs.Update
End If
row = row + 1
Loop While thereIsData
rs.Close
' Cleanup '
Set rs = Nothing
Set db = Nothing
Set xlWs = Nothing
Set xlWb = Nothing
xla.DisplayAlerts = False
xla.Quit
Set xls = Nothing
End Sub
'-----------------------------------------------------------------------------
' True if the argument is Nothing, Null, Empty, Missing or an empty string .
'-----------------------------------------------------------------------------
Public Function IsBlank(arg As Variant) As Boolean
Select Case VarType(arg)
Case vbEmpty
IsBlank = True
Case vbNull
IsBlank = True
Case vbString
IsBlank = (arg = vbNullString)
Case vbObject
IsBlank = (arg Is Nothing)
Case Else
IsBlank = IsMissing(arg)
End Select
End Function
关于excel - 导入excel文件对现有数据返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9801175/