简体   繁体   English

如何拆分此文件vb6中的数据

[英]How to split the data in this file vb6

I have this file. 我有这个文件。 It stores a names, a project, the week that they are storing the data for and hours spent on project. 它存储名称,项目,存储数据的周和项目花费的时间。 here is an example 这是一个例子

"James","Project5","15/05/2010","3"
"Matt","Project1","01/05/2010","5"
"Ellie","Project5","24/04/2010","1"
"Ellie","Project2","10/05/2010","3"
"Matt","Project3","03/05/2010","4"

I need to print it on the form without quotes. 我需要在没有引号的表单上打印它。 There it should only show the name once and then just display projects under the name. 它应该只显示一次名称,然后只显示名称下的项目。 I've looked tihs up and the split function seems interesting 我看起来很好,分裂功能似乎很有趣

any help would be good. 任何帮助都会很好。

Create a Dictionary object and then put everything you find for a given name into one dictionary entry. 创建一个Dictionary对象,然后将为给定名称找到的所有内容放入一个字典条目中。

Then in a second iteration print all that out. 然后在第二次迭代中打印出所有这些。

Microsoft has a CSV ADO provider. Microsoft有一个CSV ADO提供程序。 I think it is installed along with the rest of ADO. 我认为它与ADO的其余部分一起安装。 This is exactly the format it was designed to read. 这正是它旨在阅读的格式。

See http://www.vb-helper.com/howto_ado_load_csv.html for a VB sample. 有关VB示例,请参见http://www.vb-helper.com/howto_ado_load_csv.html

Do I understand you correctly in that you want to keep track of the names entered and thus re-order the data that way? 我是否正确理解您想要跟踪输入的名称,从而以这种方式重新排序数据? Why not just read the data into a list of some new type that has the name, project, and other information and then sort that before printing it? 为什么不将数据读入一个具有名称,项目和其他信息的新类型的列表中,然后在打印之前对其进行排序?


While the Dictionary solution is simpler, this may be a better solution if you are OK with building a class and implementing the IComparer so that you could sort the list to get this done pretty easily. 虽然Dictionary解决方案更简单,但如果您可以构建一个类并实现IComparer,那么这可能是一个更好的解决方案,这样您就可以对列表进行排序以轻松完成此操作。

You could read each line, strip out the quotes, split on the comma, then process the array of data you would be left with: 您可以读取每一行,删除引号,在逗号上拆分,然后处理您将留下的数据数组:

Dim filenum As Integer
Dim inputLine As String
Dim data() As String

filenum = FreeFile
Open "U:\test.txt" For Input As #filenum
Do While Not EOF(filenum)
  Line Input #filenum, inputLine
  inputLine = Replace(inputLine, Chr(34), vbNullString)
  data = Split(inputLine, ",")
  Debug.Print data(0), data(1), data(2), data(3)
Loop
Close #filenum

Or you could have the Input command strip the quotes, and read the data into variables: 或者您可以使用Input命令剥离引号,并将数据读入变量:

Dim filenum As Integer
Dim name As String, project As String, dat As String, hours As String

filenum = FreeFile
Open "U:\test.txt" For Input As #filenum
Do While Not EOF(filenum)
  Input #filenum, name, project, dat, hours
  Debug.Print name, project, dat, hours
Loop
Close #filenum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM