VB中的文本文件

时间:2014-05-27 20:55:39

标签: vb6

我有一个用于发送发票的文本文件。例如:IIII4444:在每张新发票之前,0000行是发票编号行,由于某种原因,0001行总是空白但是0001仍然需要在那里,而0002行是列出的项目发票。在VB6中,我想以Windows窗体的形式,使用下一个和上一个按钮逐个显示发票。任何帮助都将不胜感激。

:IIII4444:
0000 InvoiceNumber
0001
000200001 46.00     HR215.00
000200001 53.00     HR215.00
000200001 53.00     HR215.00
:IIII4444:
0000 InvoiceNumber
0001
000200001 40.00     HR48.96
000200001 40.00     HR48.96
:IIII4444:
0000 InvoiceNumber
0001
000200001 18.00     HR257.50
000200001 16.00     HR257.50
000200002 4.50      HR284.00
000200002 2.00      HR284.00
000200003 0.50      HR257.50
000200003 4.00      HR257.50
000200004 1.00      HR309.00
000200007 1.50      HR284.00
000200007 3.00      HR284.00
000200008 3.00      HR255.60
000200008 3.00      HR255.60
000200008 2.50      HR255.60
000200008 5.00      HR255.60
000200009 3.25      HR257.50
000200010 3.40      HR231.75
000200010 1.90      HR231.75
000200013 2.00      HR284.00
000200013 2.00      HR284.00
000200014 1.00      HR293.94
000200014 16.50     HR293.94
000200015 2.75      HR257.50
000200015 6.75      HR257.50
000200017 1.00      HR284.00
000200017 1.00      HR284.00
000200018 3.00      HR309.00
000200018 2.00      HR309.00
000200019 6.00      HR255.60
000200019 6.00      HR255.60

2 个答案:

答案 0 :(得分:0)

您可以将整个文件读取为1个字符串,然后使用Split()函数将其拆分为数组:

Private Sub ReadAndSplitFile(strFile As String)
  Dim intFile As Integer
  Dim strData As String
  Dim strLine() As String
  'read in the entire file
  intFile = FreeFile
  Open strFile For Input As #intFile
    strData = Input(LOF(intFile), #intFile)
  Close #intFile
  'split the lines in the file into an array
  strLine = Split(strData, vbCrLf)
End Sub

答案 1 :(得分:0)

这是一个基于行的文件,所以我打开文件进行标准的基于行的输入,并遍历它直到没有更多的行:

Dim iFileNo As Integer
Dim sLine   As String
Dim Invoice As <Whatever>

' Retrieve the next file number.
iFileNo = FreeFile

Open "Invoice.txt" For Input As #iFileNo

Do Until Eof(iFileNo)
    Line Input #iFileNo, sLine
    ProcessInvoice sLine, Invoice
Loop
ProcessInvoice "", Invoice
Close #iFileNo

ProcessInvoice的伪代码:

If the line is equal to ""
    If we are already processing an Invoice
        Finish with that Invoice
Else If the line is equal to ":IIII4444:"
    If we are already processing an Invoice
        Finish with that Invoice
    Start new invoice.
Else If the line starts with "0000"
    Save the invoice number (everything after "0000 ")
Else If the line starts with "0001"
    Do nothing
Else If the line starts with "0002"
    Call "ProcessInvoiceEntry" (everything after "00002")
    Save the invoice entry

ProcessInvoiceEntry

' The offsets are 
'
' 0        1         2         3
' 123456789012345678901234567890
'
' 00001 18.00     HR257.50
' ^    ^^        ^^
' +----++--------++------------->

Pull out each chunk from the string, and return each field as a separate string or number.

(注意Mid $()是从字符串中复制子字符串的函数。使用RTrim $()去掉任何多余的空格。)

前面的代码假定您一次加载所有发票,将它们保存在某些数据结构中 - 例如Invoice和InvoiceEntry类。另一种方法是迭代整个文件,只查看发票号,然后创建一个只包含发票号的列表,以及与之关联的行号。这样,您可以根据需要将发票加载到表单中。

相关问题