在vbscript中读取csv文件

时间:2015-11-22 06:09:30

标签: excel-vba csv vbscript vba excel

我使用以下函数来读取格式低于的文本文件。我需要查找id(column1)和返回日期(column2)。我无法理解我的代码有什么问题。 所以我的代码运行完全直到readline,但有些如何不循环。所以基本上它只是使用txt文件中的第一个SSN来检查传入的consumerSSN。 因此,如果我传递getDOH(213396391),它会返回空白(“”),而不是相应的日期。

0

3 个答案:

答案 0 :(得分:3)

一旦找到要搜索的值,就应该退出循环。

未测试:

Function getDOH(consumerSSN)
    dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
    dim doh, t, x, f

    f = fso.GetAbsolutePathName("..\Test Files\") & "\sraDOH.txt"

    If fso.fileexists(f) then 

        Set tsIn2 = fso.OpenTextFile(File, 1)
        Do While Not tsIn2.AtEndOfStream
            doh = tsIn2.ReadLine
            t = split(doh,",")
            If consumerSSN = t(0) Then
                getDOH = t(1)
                exit do
            End If
        Loop

    End If

End Function

答案 1 :(得分:2)

此外:您永远不应该比较VBScript中不同(子)类型的值。所以你的电话getDOH(213396391)有风险;它应该是getDOH("213396391"),因为Split()会产生字符串。

证据:

>> Function doCmp(v)
>>   s = "213396391,2015-06-01 00:00:00"
>>   t = Split(s, ",")
>>   doCmp = v = t(0)
>> End Function
>> WScript.Echo CStr(doCmp(213396391)), CStr(doCmp("213396391"))
>>
False True

答案 2 :(得分:0)

以下功能基于我用来查找旧产品ID并将其替换为新系统的产品ID的类似功能。这还要求输入文件具有标题行,例如:

'sraDOH.txt:
'SSN, DOH
'578171533,2015-01-01 00:00:00
'213396391,2015-06-01 00:00:00
'077966385,2015-01-01 00:00:00
'216418521,2015-01-01 00:00:00

Function getDOH(consumerSSN)

Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim iConnection                 'Connection objects for input
Dim iRecordset                  'RecordSets for input
Dim dDOH                        ' date to look up   
Dim sInputFileDir               ' Directory containing the TXT file to process
Dim sInputTXT                   ' Name of the TXT file to open (without a pathname)

sInputFileDir = fso.GetAbsolutePathName("..\Test Files\")
sInputTXT = "sraDOH.txt"

'
'Open text file
'   

Set iConnection = CreateObject("ADODB.Connection")
Set iRecordset = CreateObject("ADODB.Recordset")

' Open a txt file using ODBC
iConnection.Provider = "MSDASQL"
iConnection.Open "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & sInputFileDir & "\;"


iRecordset.Open "SELECT DOH FROM sraDOH.txt Where SSN = "& consumerSSN, _
        iConnection, adOpenStatic, adLockOptimistic, adCmdText

dDOH = iRecordset("DOH")

If IsNull(dDOH) Or dDOH = "" Then
    stdout.WriteLine "ERROR: Bad date on record for: "& consumerSSN
End If

iRecordset.Close
iConnection.Close

getDOH = dDOH

End Function    

这是我原来的功能:

Function setInvoiceLineItemRefListID(dpassedProductID)

Dim iConnection                 'Connection objects for input
Dim iRecordset                  'RecordSets for input
Dim dProductID                  'Foundry Product ID
Dim dListID                     'Quickbooks Online Product ID
Dim sInputFileDir               ' Directory containing the CSV file to process
Dim sInputCSV                   ' Name of the CSV file to open (without a pathname)

' Spreadsheet columns
'productID, sproductAbbrev, sechoDataItemNumber, sQBItemCode, slistID

sInputFileDir = "C:\ThinkwellApps\Programs"

sInputCSV = "products.csv"          'Name of the spreadsheet containing the list of ProductIDs

'
'Open spreadsheet
'   

Set iConnection = CreateObject("ADODB.Connection")
Set iRecordset = CreateObject("ADODB.Recordset")

' Open a CSV file  
iConnection.Provider = "MSDASQL"
iConnection.Open "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & sInputFileDir & "\;"


iRecordset.Open "SELECT ListID FROM products.csv Where ProductID = "& dpassedProductID, _
        iConnection, adOpenStatic, adLockOptimistic, adCmdText

dListID = iRecordset("ListID")

If IsNull(dListID) Or dListID = "" Then
    stdout.WriteLine "ERROR: Bad listID for: " & dpassedProductID

iRecordset.Close
iConnection.Close

setInvoiceLineItemRefListID = dListID
End Function