我必须将XLS文件中的数据记录为文本,因为我需要在左边有零
我的XLS文件如下所示:
某些单元格有错误: 数字格式化为文本或由撇号处理 这是VB6中为null的单元格
Private Sub Command1_Click()
Dim oRs As ADODB.Recordset
Dim oConn As ADODB.Connection
Dim sConString As String
Dim sXLFile As String
Dim vValue As Variant
sXLFile = "T:\BC\dev\howto_read_excel\ArtigosCampanha\Promoção Mês Criança_1_2015_2015 05 25_2015 06 28_CM.xls"
sConString = "Provider= Microsoft.Jet.OLEDB.4.0;" & " Data Source=" & sXLFile & ";Extended Properties=Excel 8.0;"
Set oConn = New ADODB.Connection
With oConn
.CursorLocation = adUseClient
.Open sConString
End With
Set oRs = New ADODB.Recordset
With oRs
.CursorType = adOpenStatic
.CursorLocation = adUseClient
.LockType = adLockPessimistic
.Source = "SELECT * FROM [Sheet1$]"
.ActiveConnection = oConn
.Open
.MoveFirst
End With
Dim refer As String
Dim cb As String
Do While Not oRs.EOF
' oRS ("CodBarras")
refer = IIf(IsNull(oRs.Fields.item("Referencia").value), "NULL", oRs.Fields.item("Referencia").value)
cb = IIf(IsNull(oRs.Fields.item("CodBarras").value), "NULL", oRs.Fields.item("CodBarras").value)
'use refer and cb where with some function
oRs.MoveNext
Loop
oRs.Close
oConn.Close
Set oRs = Nothing
Set oConn = Nothing
End Sub
有人可以帮助我吗?
答案 0 :(得分:0)
如果流氓值始终可以转换为真实数字,您可以将所有CodBarras字段项目作为文本引入或将所有这些项目作为数字引入。正在尝试避免崩溃的开销会丢弃看起来像文本的文本。
'as text
.Source = "SELECT [Referencia], CStr([CodBarras]) FROM [Sheet1$]"
'as numbers
.Source = "SELECT [Referencia], CLng([CodBarras]) FROM [Sheet1$]"
这些是VBA / MS Access转换代码。 T-SQL将接近,
'as text
.Source = "SELECT [Referencia], CONVERT(varchar(16), [CodBarras]) FROM [Sheet1$]"
'as numbers
.Source = "SELECT [Referencia], CONVERT(int, [CodBarras]) FROM [Sheet1$]"