Vb.net 用于读写的VBA文本文件输入
我在VB 2010中使用一个按钮来编写表单,这完全是个白痴。我已经有一段时间没有使用VB了,我注意到2010年与几年前使用VB相比有了很多变化 我需要做的是从一个文件中读取,然后在写入原始文件的同时写入两个新的独立文件。它将从文本文件中读取一些内容并比较当前日期 文本内容将是一个序列,接下来的两列将是日期Vb.net 用于读写的VBA文本文件输入,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,我在VB 2010中使用一个按钮来编写表单,这完全是个白痴。我已经有一段时间没有使用VB了,我注意到2010年与几年前使用VB相比有了很多变化 我需要做的是从一个文件中读取,然后在写入原始文件的同时写入两个新的独立文件。它将从文本文件中读取一些内容并比较当前日期 文本内容将是一个序列,接下来的两列将是日期 Dim iFileName As New StreamReader("G:\SAP In and Out\test.txt", False) Dim sFileExport A
Dim iFileName As New StreamReader("G:\SAP In and Out\test.txt", False)
Dim sFileExport As New StreamWriter(DateValue(Now)) + " SAP Export", False)
Dim sFileImport As New StreamWriter(DateAdd(DateInterval.Day, 10, DateValue(Now)) + " SAP Import", False)
Dim VTS As String 'Will be used to grab the VT serial
Dim CurrentDate As Date 'Will be used to compare to grabbed dates
Dim NextDateOut As Date 'Will be used to grab next date out value
Dim NextDateIn As Date 'Will be used to grab next date in value
'Setting a consistant value with daily'
CurrentDate = DateValue(Now)
Do While Not EOF(1)
Input(iFileName),VTS,NextDateOut,NextDateIn
If CurrentDate = NextDateOut Then
'Write to the export File
sFileExport.write(VTS)
'Write under the just read line in the open file
iFileName.write(/newline + VTS + /TAB + (DateAdd(DateInterval.Day, 20, DateValue(Now)) + /tab + (DateAdd(DateInterval.Day, 30, DateValue(Now)))
ElseIf CurrentDate = NextDateIn Then
'Write to import file
sFileImport.Write(VTS)
End If
Loop
End Sub
但我的语法是关闭的,显然它没有运行,因为我请求帮助。
请提前帮助和感谢。我已经为此工作了几个小时,还没有任何积极的结果。我想现在就可以了,但我必须添加一个额外的功能,以便测试它。 我收到日期格式错误,因为我希望您使用美国日期格式(MM/DD/YYYY),而我使用英国日期格式(DD/MM/YYYY) 无论如何,你可以去掉这个函数,因为我认为你不需要它,但我还是保留了它,这样你就可以看到它发生了什么,它非常简单,可以在日期格式之间进行转换,尽管由于你的日期和月份不是两位数长(没有前导零),这使得转换更加困难 我希望它能为你指明正确的方向,你可以根据自己的喜好来改变它
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim iFileName As New System.IO.StreamReader("c:\temp\vttest.txt", False)
Dim iReadData As List(Of String)
Dim Buffer As String
'Read complete file into array
iReadData = New List(Of String)
Do While Not iFileName.EndOfStream()
Try
Buffer = ""
Buffer = iFileName.ReadLine()
iReadData.Add(Buffer)
Catch ex As Exception
'error or end of file
End Try
Loop
iFileName.Close()
'Not needed
'Dim VTS As String 'This is in iLineData(0) after we split the line
'Dim NextDateOut As Date 'This is in iLineData(1) after we split the line
'Dim NextDateIn As Date 'This is in iLineData(2) after we split the line
'Process
Dim iFileUpdated As New System.IO.StreamWriter("c:\temp\vttest_copy.txt", True)
Dim sFileExport As New System.IO.StreamWriter("c:\temp\vttest" & Replace(DateValue(Now), "/", "_") & " SAP Export.txt", True)
Dim sFileImport As New System.IO.StreamWriter("c:\temp\vttest" & Replace(DateAdd(DateInterval.Day, 10, DateValue(Now)), "/", "_") + " SAP Import.txt", True)
Dim iLineData() As String
'Setting a consistant value with daily'
Dim CurrentDate As Date = DateValue(Now)
Dim RowId = 0
Do While RowId < iReadData.Count()
iLineData = Split(iReadData(RowId), " ")
iFileUpdated.WriteLine(iLineData(0) & " " & iLineData(1) & " " & iLineData(2))
If CurrentDate = FormatDate(iLineData(1), "US", "UK") Then
'Write to the export File
sFileExport.WriteLine(iLineData(0))
'Write under the just read line in the open file
iFileUpdated.WriteLine(iLineData(0) & " " & (DateAdd(DateInterval.Day, 20, DateValue(Now))) & " " & (DateAdd(DateInterval.Day, 30, DateValue(Now))))
ElseIf CurrentDate = FormatDate(iLineData(2), "US", "UK") Then
'Write to import file
sFileImport.WriteLine(iLineData(0))
End If
RowId = RowId + 1
Loop
sFileExport.Close()
sFileImport.Close()
iFileUpdated.Close()
MsgBox("Finshed")
End Sub
'This section added because my PC is set to DD/MM/YYYY (UK)
'Expect yours is set to MM/DD/YYYY (US)
Function FormatDate(ThisDate As String, ThisFormat As String, ThisTargetFormat As String) As Date
Dim X As Integer = 0
Dim Day1 As Integer = 0
Dim Month1 As Integer = 0
Dim Year1 As Integer = 0
Dim Buf As String = ""
If ThisFormat = "US" Then
For X = 1 To Len(ThisDate)
If Mid(ThisDate, X, 1) = "/" Then
If Month1 > 0 Then
Day1 = Buf
Buf = ""
Else
Month1 = Buf
Buf = ""
End If
Else
Buf = Buf & Mid(ThisDate, X, 1)
End If
Next
Year1 = Buf
Else
For X = 1 To Len(ThisDate)
If Mid(ThisDate, X, 1) = "/" Then
If Day1 > 0 Then
Month1 = Buf
Buf = ""
Else
Day1 = Buf
Buf = ""
End If
Else
Buf = Buf & Mid(ThisDate, X, 1)
End If
Next
Year1 = Buf
End If
'reformat for output
If ThisFormat = "US" Then
If ThisTargetFormat = "US" Then
'USA
FormatDate = (Format(Month1, "00") & "/" & Format(Day1, "00") & "/" & Format(Year1, "0000"))
Else
'UK
FormatDate = (Format(Day1, "00") & "/" & Format(Month1, "00") & "/" & Format(Year1, "0000"))
End If
Else
If ThisTargetFormat = "US" Then
'USA
FormatDate = (Format(Month1, "00") & "/" & Format(Day1, "00") & "/" & Format(Year1, "0000"))
Else
'UK
FormatDate = (Format(Day1, "00") & "/" & Format(Month1, "00") & "/" & Format(Year1, "0000"))
End If
End If
End Function
VTSTEST.TXT的副本(修改后)
注:我希望这两行较长的文字是插入现有四行文字之间的文字
VTTEST06\u 04\u 2013 SAP Import.Txt
这个文件是空的
VTTEST27\u 03\u 2013 SAP Export.TXT
VT2000
VT2000
你可以发布一个输入文本文件的示例行吗?对不起,我犯了一个错误,我以为你只是在使用输入文件进行阅读(而不是写作)对不起,忘了这么做,示例文本文件输入看起来与此类似:VT1000 3/26/2013 4/5/2013 VT1100 3/26/2013 4/5/2013 VT2000 3/27/2013 4/6/2013 VT2200 3/27/2013 4/6/2013 4/6/2013我似乎无法将其用于新行,但在第二次日期之后,它是VT的下一行,等等。首先非常感谢您!!!这似乎是正确的方向。我正在浏览它,并在其中挑选一些。它运行,但导出和导入文件是空的,我仍在试图找出一个。我在想,如果我是对的,我可能需要做一些数据类型转换之类的事情?
VT1000 3/26/2013 4/5/2013
VT1100 3/26/2013 4/5/2013
VT2000 3/27/2013 4/6/2013
VT2000 16/04/2013 26/04/2013
VT2200 3/27/2013 4/6/2013
VT2200 16/04/2013 26/04/2013
VT2000
VT2000