2012-08-07 63 views
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 

的片断我怎样才能得到有关我的代码每一部分阅读此,除了打破它,然后创建一个显示结果的表。

回答

1

考虑,你需要以一个新行添加到表从variableArray两个值,我会做一个FOR..NEXT环路(由2步进),而不是对于...每个:

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次。每次迭代你将创建两个单元格,当前值和当前值数组中的下一个变量。然后,这些单元格将被添加到一行中,然后该行将被添加到表格中。

1

声明一个DataTable

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,你可以将它绑定到一个在HTML意义上是一个表格的GridView。

希望这会有所帮助。