vb.net - 从文本文件中查找经过的时间
问题描述
因此,我从文本文件中导入数据,并根据某些信息将它们排序到不同的列表框中。数据示例如下;但是我需要找到条目的经过时间。所以我需要从第一个条目时间戳中减去最后一个条目时间戳。我不确定如何从列表框或文本文件的第一行和最后一行中提取特定数据,我还需要对两个单独的单元格执行此操作。
例如: 这是第一个生产单元:
生产单元 1,2018 年 4 月 20 日下午 2:09:18,340
生产单元 1,2018 年 4 月 20 日下午 3:13:48,211
第二个单元格:
生产单元 2,2018 年 4 月 20 日下午 2:09:24,531
生产单元 2,2018 年 4 月 20 日下午 3:13:45,720
请注意,两个单元格在同一个文本文件中混杂在一起,而不是按顺序排列
当前基于颜色的排序:仍然需要:经过的时间、重量和 Go/NoGo 基于最后的值,我还没有尝试过。
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "txt files (*.txt)|*.txt"
OpenFileDialog1.FilterIndex = 1
OpenFileDialog1.RestoreDirectory = True ' Returns to original start directory
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
m_FileName = OpenFileDialog1.FileName
srFile = New StreamReader(m_FileName) ' Need new instance of the object, this is necessary
While Not srFile.EndOfStream
strLine = srFile.ReadLine
If strLine.Contains("Production Cell 1") Then
lstProductionCell1.Items.Add(strLine)
If strLine.Contains("PM, 1") Or strLine.Contains("AM, 1") Then
white1 += 1
lblWhiteCell1.Text = CStr(white1)
ElseIf strLine.Contains("PM, 2") Or strLine.Contains("AM, 2") Then
black1 += 1
lblBlackCell1.Text = CStr(black1)
ElseIf strLine.Contains("PM, 3") Or strLine.Contains("AM, 3") Then
red1 += 1
lblRedCell1.Text = CStr(red1)
ElseIf strLine.Contains(" PM, 4") Or strLine.Contains("AM, 4") Then
yellow1 += 1
lblYellowCell1.Text = CStr(yellow1)
ElseIf strLine.Contains("PM, 5") Or strLine.Contains("AM, 5") Then
green1 += 1
lblGreenCell1.Text = CStr(green1)
ElseIf strLine.Contains("PM, 6") Or strLine.Contains("AM, 6") Then
blue1 += 1
lblBlueCell1.Text = CStr(blue1)
ElseIf strLine.Contains("PM, 7") Or strLine.Contains("AM, 7") Then
brown1 += 1
lblBrownCell1.Text = CStr(brown1)
ElseIf strLine.Contains("PM, 8") Or strLine.Contains("AM, 8") Then
grey1 += 1
lblGreyCell1.Text = CStr(grey1)
End If
ElseIf strLine.Contains("Production Cell 2") Then
lstProductionCell2.Items.Add(strLine)
If strLine.Contains("PM, 1") Or strLine.Contains("AM, 1") Then
white2 += 1
lblWhiteCell2.Text = CStr(white2)
ElseIf strLine.Contains("PM, 2") Or strLine.Contains("AM, 2") Then
black2 += 1
lblBlackCell2.Text = CStr(black2)
ElseIf strLine.Contains("PM, 3") Or strLine.Contains("AM, 3") Then
red2 += 1
lblRedCell2.Text = CStr(red2)
ElseIf strLine.Contains(" PM, 4") Or strLine.Contains("AM, 4") Then
yellow2 += 1
lblYellowCell2.Text = CStr(yellow2)
ElseIf strLine.Contains("PM, 5") Or strLine.Contains("AM, 5") Then
green2 += 1
lblGreenCell2.Text = CStr(green2)
ElseIf strLine.Contains("PM, 6") Or strLine.Contains("AM, 6") Then
blue2 += 1
lblBlueCell2.Text = CStr(blue2)
ElseIf strLine.Contains("PM, 7") Or strLine.Contains("AM, 7") Then
brown2 += 1
lblBrownCell2.Text = CStr(brown2)
ElseIf strLine.Contains("PM, 8") Or strLine.Contains("AM, 8") Then
grey2 += 1
lblGreyCell2.Text = CStr(grey2)
End If
End If
End While
End If
srFile.Close() ' Be sure to close the file
End Sub
解决方案
代码的逻辑对我来说是陌生的。我不明白您试图确定白色、黑色、灰色等颜色值的基础是什么。所以我并没有试图纠正它。
无论如何,根据您描述的关于从文本文件中识别单元格日期之间差异的问题,我试图提出以下解决方案。
我编写了一个示例控制台应用程序来复制用例。
Imports System
Imports System.Globalization
Imports System.Collections.Generic
Imports System.Linq
Public Class Program
Public Shared Sub Main()
Dim inputData = New List(Of String)()
'I am using generic collection here for the input data instead of file.
'You can use File.ReadAllLines method to read all the lines from file and store them in an array.
' Dim inputData = File.ReadAllLines(m_FileName)
inputData.Add("Production Cell 1, 4/20/2018 2:09:18 PM, 340")
inputData.Add("Production Cell 1, 4/20/2018 3:13:48 PM, 211")
inputData.Add("Production Cell 2, 4/20/2018 2:09:24 PM, 531")
inputData.Add("Production Cell 2, 4/20/2018 3:13:45 PM, 720")
Dim cellDates = New Dictionary(Of String, List(Of DateTime))()
Dim prodCell1 As String = "Production Cell 1"
Dim prodCell2 As String = "Production Cell 2"
For Each item In inputData
Dim lineItems = item.Split({","c})
Dim datePart = lineItems(1).Trim() & lineItems(2)
Dim dateValue = DateTime.ParseExact(datePart, "M/dd/yyyy h:mm:ss tt fff", CultureInfo.InvariantCulture)
If Not cellDates.ContainsKey(lineItems(0)) Then
cellDates.Add(lineItems(0), New List(Of DateTime)())
End If
cellDates(lineItems(0)).Add(dateValue)
Next
'Sorting dates of both the Cells.
Dim prodCell1Dates = cellDates(prodCell1).OrderBy(Function(dt) dt)
Dim prodCell2Dates = cellDates(prodCell2).OrderBy(Function(dt) dt)
'You can add the dates values in the listboxes here as following.
'lstProductCell1.DataSource = cellDates(prodCell1)
'lstProductCell2.DataSource = cellDates(prodCell2)
'Getting difference between the last and the first date.
'This returns an instance of TimeSpan
Dim prodCell1Duration = prodCell1Dates.Last() - prodCell1Dates.First()
Dim prodCell2Duration = prodCell2Dates.Last() - prodCell2Dates.First()
'Display difference in the form of total minutes in the Console.
'You need to write code here to display values in the proper labels.
Console.WriteLine(prodCell1Duration.TotalMinutes)
Console.WriteLine(prodCell2Duration.TotalMinutes)
End Sub
End Class
有用的链接 https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx
这将帮助您解决问题。
推荐阅读
- flutter - 如何在 Flutter 的粘性标题正文中添加 Gridview
- ruby-on-rails - ActiveAdmin:将默认主题样式应用于自定义面板的自定义部分
- r - 用每个盒子的子组数和观察值标记箱线图
- asp.net-mvc - iframe 可以安全地用于显示来自包含它的同一应用程序的内容吗?
- servlets - 使用 classes/project/servlet 在 tomcat9 中的浏览器中的 host_name 上进行 URL 重定向
- php - 更改 google Pay 礼品卡中的文字
- mysql - 根据 id 从表中的列中查找重复项,他们做了多少事务
- c - 如何通过 GO 中的编码包打包 C 位字段结构?
- java - ComponentScan 和 bean 的自动装配似乎在 Spring 应用程序中不起作用
- python - Selenium 通过类名的一部分或其他属性选择元素