将Visual Basic 2010中的表保存为.txt文件

时间:2012-11-16 10:29:18

标签: vb.net

我希望得到在下面程序结束时生成的表可以选择保存到.txt中,以便以后可以回顾它但是我很难让这个发生

    Module Module1
    Dim kTick As Integer
    Dim kName(64) As String
    Dim kHours(64) As Integer
    Dim kHoursPay(64) As Integer
    Dim kGross(64) As Integer
    Dim kTax As Integer = 20
    Dim kNet(64) As Integer
    Dim kTotal As Integer
    Dim kAverage As Integer
    Sub Main()
        'Assigning kTick (Number of employee's)
        Assign()
        'Imputting the data
        Entry()
        'Calculating kGross, kTax, kNet and kTotal
        Print()
    End Sub
    Sub Assign()
        Console.ForegroundColor = ConsoleColor.Gray
        Console.WriteLine("Please enter the total amount of employee's:")
        Console.ForegroundColor = ConsoleColor.White
        kTick = Console.ReadLine()
        Console.Clear()
    End Sub
    Sub Entry()
        For kCounter = 1 To kTick
            Console.ForegroundColor = ConsoleColor.DarkGray
            Console.WriteLine("Employee " & kCounter)
            Console.ForegroundColor = ConsoleColor.Gray
            Console.WriteLine("Please enter the employee name below:")
            Console.ForegroundColor = ConsoleColor.White
            kName(kCounter) = Console.ReadLine()
            Do
                Console.ForegroundColor = ConsoleColor.Gray
                Console.WriteLine("Please enter the employees total hours worked below:")
                Console.ForegroundColor = ConsoleColor.White
                kHours(kCounter) = Console.ReadLine()
            Loop Until kHours(kCounter) >= 0 And kHours(kCounter) <= 60
            Do
                Console.ForegroundColor = ConsoleColor.Gray
                Console.WriteLine("Please enter the employees Hourly Pay below:")
                Console.ForegroundColor = ConsoleColor.White
                kHoursPay(kCounter) = Console.ReadLine()
            Loop Until kHoursPay(kCounter) >= 6 And kHoursPay(kCounter) <= 250
            Console.Clear()
        Next
    End Sub
    Sub Print()
        For kCounter = 1 To kTick
            kGross(kCounter) = kHours(kCounter) * kHoursPay(kCounter)
        Next
        For kCounter = 1 To kTick
            kNet(kCounter) = (kGross(kCounter) / 10) * 8
        Next
        For kCounter = 1 To kTick
            kTotal = kTotal + kHours(kCounter)
        Next
        kAverage = kTotal / kTick
        Console.ForegroundColor = ConsoleColor.Gray
        Console.WriteLine("Name" & vbTab & "Hours" & vbTab & "Hourly Rate" & vbTab & "Gross Pay" & vbTab & "Tax" & vbTab & "Net Pay")
        Console.ForegroundColor = ConsoleColor.White
        For kCounter = 1 To kTick
            Console.WriteLine(kName(kCounter) & vbTab & kHours(kCounter) & vbTab & "£" & kHoursPay(kCounter) & vbTab & vbTab & "£" & kGross(kCounter) & vbTab & vbTab & kTax & "%" & vbTab & "£" & kNet(kCounter))
        Next
        Console.ForegroundColor = ConsoleColor.Gray
        Console.WriteLine("Total hours worked: " & kTotal)
        Console.WriteLine("Total average hours worked: " & kAverage)
        Console.WriteLine("Total number of employees: " & kTick)
        Console.ReadLine()
        Save()
    End Sub
    Sub Save()

    End Sub
End Module

我正在尝试在Save子例程中获取代码,任何帮助都将不胜感激!

谢谢Kai

2 个答案:

答案 0 :(得分:1)

一个注意事项:您不应Save来自Print,因为两者本质上都不相关。因此,您可能希望打印而不保存或保存而无需打印到控制台。

一种简单的方法是使用File类,例如使用File.WriteAllText(path)File.WriteAllLines(path)。因此,您需要在某处存储要输出的文本(到控制台和文件)。例如,在List(Of String)变量中。

答案 1 :(得分:0)

您正在寻找StreamWriter课程。它有WriteWriteLine方法可以帮助您。它的工作方式与您使用的Console.WriteLine相同。