0

我有这个代码

'Open a file for reading
'Get a StreamReader class that can be used to read the file
Dim objStreamReader As StreamReader
Dim variableArray(20) As String
objStreamReader = File.OpenText(filePath)

'Read one line at a time
Dim someString As String
Dim variableNum As Integer = 0
'Iterate through lines
While objStreamReader.Peek() <> -1
    someString = objStreamReader.ReadLine()
    variableArray(variableNum) = someString
    variableNum = variableNum + 1
End While
For Each line As String In variableArray

Next
objStreamReader.Close()

我有一个在日志文件中输出结果的 vbscript,附加在每一行并由“|”分隔 只有两列。

这是VBScript代码的片段

f1.WriteLine("Server Name " & "|" & strName)
f1.WriteLine("OS Name: " & "|" & strCaption)
f1.WriteLine("OS Version: " & "|" & strVersion
f1.WriteLine("CSD Version: " & "|" & strCSDVer
f1.WriteLine("Serial Number: " & "|" & strSerial

我怎样才能让我的代码的 For Each 部分来阅读它,将其分解,然后创建一个显示结果的表格。

4

2 回答 2

1

考虑到您需要 variableArray 中的两个值才能将新行添加到表中,我会执行 For..Next 循环(步进 2)而不是 For...Each:

Dim myTable As New Table
Dim loopCount As Integer

For loopCount = 0 To variableNum Step 2

    Dim myRow As New TableRow
    Dim myCell1 As New TableCell
    Dim myCell2 As New TableCell

    myCell1.Text = variableArray(loopCount)
    myCell2.Text = variableArray(loopCount + 1)
    myRow.Cells.Add(myCell1)
    myRow.Cells.Add(myCell2)
    myTable.Rows.Add(myRow)

Next

由于您已经将数组中的元素数量存储在“variableNum”中,因此您可以从 0 循环到该值,步长为 2。每次迭代您将创建两个单元格,其中包含当前和下一个变量的值大批。然后,这些单元格将被添加到一行中,该行又将添加到表格中。

于 2012-08-07T16:34:49.493 回答
1

声明一个数据表

Dim table As DataTable = new DataTable("MyTable")

现在在 Foreach 内部:

Dim LineArray() As String = Split(line, "|") 'This will break apart each line into its two parts
'Now add each item of LineArray to the datatable. AKA
Dim column As DataColumn = new DataColumn()
column.DataType = System.Type.GetType("System.Int32")
column.ColumnName = LineArray(0)
column.ReadOnly = True
column.Unique = True
table.Columns.Add(column)

Dim row As DataRow 
row = table.NewRow()
row(LineArray(0)) = LineArray(1)
table.Rows.Add(row)

我不知道您尝试实现的整个逻辑,但这应该会给您一个良好的开端。它基本上取第一列并将其设置为表中的列,然后取第二列并将其设为行值。

一旦你构建了你的 DataTable,你就可以将它绑定到一个 GridView,它在 HTML 意义上是一个表。

希望这可以帮助。

于 2012-08-07T16:38:32.047 回答