【发布时间】:2014-04-22 11:02:04
【问题描述】:
我目前使用以下 ADODB 连接字符串从 Excel 工作簿中检索数据(oConnection 已声明为新的 ADODB.Connection,oRecordSet 已声明为新的 ADODB.RecordSet):
With oConnection
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=C:\Myfile.xlsx;Mode=Read;Extended Properties=""Excel 12.0;HDR=Yes;"""
.Open
Set oRecordSet = .Execute(mySqlStatement)
End With
我将记录集添加到数组中,一切似乎都运行良好。但是,如果 Excel 工作表中的单元格包含超过 255 个字符,则文本将被截断/超过 255 个的字符将被截断。
我用来连接 Excel 文件的整个函数如下:
Public Function RecordSet2Matrix(mSqlStatement As String) As Variant
Dim oSettingsFolder As String
Dim oSettingsFile As String
Dim oConnection As New ADODB.Connection
Dim oRecordSet As New ADODB.Recordset
Dim oRecordArray As Variant
Dim oTempArray As Variant
Dim x As Long
Dim y As Long
Dim xUpper As Long
Dim yUpper As Long
'Error handling
On Error GoTo errHandler
'Get location and settings file
oSettingsFolder = GetFolder(xFolderEnum.FolderOther)
oSettingsFile = xSettingsFile
oTempArray = Array()
'Check if settings file exists
If Len(Dir(oSettingsFolder & oSettingsFile)) = 0 Then
RecordSet2Matrix = oTempArray
Exit Function
End If
'Create connection to settings file
With oConnection
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & oSettingsFolder & oSettingsFile & _
";Mode=Read;Extended Properties=""Excel 12.0;HDR=Yes;"""
.Open
Set oRecordSet = .Execute(mSqlStatement)
End With
'Read the settings file and add data to an array
If oRecordSet.EOF Then
oTempArray = Array()
Else
oRecordArray = oRecordSet.GetRows
xUpper = UBound(oRecordArray, 2)
yUpper = UBound(oRecordArray, 1)
'Resize array to fit all records
ReDim oTempArray(xUpper, yUpper)
For x = 0 To xUpper
For y = 0 To yUpper
oTempArray(x, y) = oRecordArray(y, x)
Next y
Next x
End If
'Close connection to file
If CBool(oRecordSet.State And adStateOpen) Then
oRecordSet.Close
End If
'Return record set
RecordSet2Matrix = oTempArray
errHandler:
If Err.Number <> 0 Then
ShowErrorMessage Err.Number, Err.Description, "modADO.RecordSet2Matrix"
Set oRecordSet = Nothing
RecordSet2Matrix = oTempArray
End If
cleanUp:
Set oRecordSet = Nothing
Set oTempArray = Nothing
End Function
然后我运行实际的 SELECT 语句以从 Excel 中获取数据:
Public Function GetFirmDetails(mFirmName As String) As xIniDataRecord
Dim oSqlStatement As String
Dim oDataArray As Variant
Dim oReturnRec As xIniDataRecord
'Error handling
On Error GoTo errHandler
'SQL query for retrieving office details
oSqlStatement = "SELECT [FirmDisplayName], [FirmPrintName], [FirmInfoA]" & _
"FROM [Firms$] " & _
"WHERE [FirmDisplayName] = '" & mFirmName & "';"
'Return query to array
oDataArray = RecordSet2Matrix(oSqlStatement)
'Exit function if array is empty
If UBound(oDataArray) < 0 Then
GetFirmDetails = oReturnRec
Exit Function
End If
'Return array data to return record
With oReturnRec
.FirmDisplayName = oDataArray(0, 0)
.FirmName = oDataArray(0, 1)
.FirmInfoA = oDataArray(0, 2)
End With
'Return data record
GetFirmDetails = oReturnRec
errHandler:
If Err.Number <> 0 Then
ShowErrorMessage Err.Number, Err.Description, "modADO.GetFirmDetails"
GetFirmDetails = oReturnRec
End If
cleanUp:
Set oDataArray = Nothing
End Function
我一直在研究与我类似的其他问题,但没有真正找到解决方案。
有人知道如何避免 255 个字符的限制吗?
注意!我正在运行 Excel 2007。
【问题讨论】:
-
您的意思是您的
mySqlStatement的长度超过255 个字符但.Execute()方法只使用前255 个字符? -
这意味着在我的 Excel 文件中,我的单元格包含超过 255 个字符。但由于某种原因,只返回每个单元格的前 255 个字符。
-
我不确定我是否有足够的信息来重现此问题...您正在执行的 SQL 语句是什么?您如何将数据从记录集中复制到工作表?你能提供你使用的数据样本吗?
-
我添加了用于从 Excel 中提取的函数。基本上,Excel 文件包含一个名为“Firms”的工作表,其中包含名为 FirmDisplayName、FirmPrintName 和 FirmInfoA 的三列。