有没有办法通过vbscript从csv文件中提取数据?

时间:2014-04-02 20:22:08

标签: html vbscript

我想创建一个这样的html页面:

enter image description here

File Type下拉框将是4个csv文件,其中包含powershell脚本输出的数据,如下所示:

enter image description here

有没有办法根据yearmonth下拉列表选择使用vbs从csv文件中提取行?例如创建时间或lastwrite时间。

或者是否有更好的想法这样做?

我现在看起来像这样,我必须输入一个字符串进行搜索,我想使用下拉列表......

enter image description here

<html>
<head>
<title>TEST</title>

</head>

<script language="VBScript">
    'Closes all elements of program in an orderly fashion.
    sub sub_exit
        Self.Close()
    end sub

    'this sub does all the work
    sub sub_query_database_OnClick()
        If UserInputQuery.Value = "" Then
            MsgBox "Please enter a search term."
            UserInputQuery.Focus
            Exit Sub
        End If
        outputArea.Value = ""
        'Declare all variables
        Dim strfind
        Dim strComputer
        Dim testValue
        Dim objExcel
        Dim objWorkbook
        Dim intRow
        Dim intColumn
        Dim strExcel
        Dim matchArray(10)
        Dim intCount
        Dim outputBox

        'initialize all variables
        strComputer = "."
        strfind = UCase(UserInputQuery.Value)
        strFile = "C:\Work\Web\Data\nxdata.csv"
        strExcel = UCase(strFile)
        Set objExcel = CreateObject("Excel.Application")

        'refresh database
        objExcel.Visible = False
        objExcel.Application.DisplayAlerts = false
        Set objWB = objExcel.Workbooks.Open(strExcel,2,False)
        Set objSheet = objWB.Sheets(1)
        objWB.RefreshAll



        Dim FoundCell
        Dim LastCell
        Dim FirstAddr
        Dim strRange

        strRange = "a3:E2000"
        intCount = 0

        With objSheet.Range(strRange)
            Set LastCell = .Cells(.Cells.Count)
        End With
        Set FoundCell = objSheet.Range(strRange).Find(strfind, LastCell)
        If Not FoundCell Is Nothing Then
            FirstAddr = FoundCell.Address
        End If
        Do Until FoundCell Is Nothing
            intCount = intCount + 1
            outputArea.Value = outputArea.Value & objSheet.Cells(FoundCell.Row, 2).Value & " " & objSheet.Cells(FoundCell.Row, 5).Value & " " & objSheet.Cells(FoundCell.Row, 6).Value & vbCr
            Set FoundCell = objSheet.Range(strRange).FindNext(FoundCell)
            If FoundCell.Address = FirstAddr Then
                Exit Do
            End If
        Loop
        If intCount = 0 Then
            MsgBox "No occurrences of " & strFind & " were found."
        Else
            MsgBox intCount & " occurrences of " & strFind & " were found."
        End If

        objWB.Saved = True
        objWB.Close True
        'excel program is closed.
        objExcel.Quit
    end sub
</script>

<body>
<!--Here we have a textfield for input, an input button, a textfield for output, and an exit button.-->
<input type="text" name="UserInputQuery" size="50">
<input type="button" value="Run Query" name="sub_query_database"><p>

<input type="button" value="Exit" name="exitButton"  onClick="sub_exit"><p>
<br>
<textarea name="outputArea" rows="50" cols="225"></textarea>

</body>
</html>   

1 个答案:

答案 0 :(得分:1)

您可以像连接SQL Server或Access数据库一样连接到结构化文本文件(如CSV)。然后,您可以使用SQL查找匹配的记录。例如:

Sub RunSearch_OnClick()

    With CreateObject("ADODB.Connection")

        .Open "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=C:\MyFolder\;Extensions=csv;"

        Dim Recordset
        Set Recordset = .Execute("select * from YourFile.csv where year='2014' and month='4'")

        ' Etc ...

    End With

End Sub