vb读取csv文件,在文本框中输出
例如:csv文件内容为:Date,$Time,PV1,PV2,PV3,PV11,PV12,PV13,PV4,SP,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,YOUYA,VZ1,V2,V3,V4,V503/23/12,17:40:26,102,102,102,102,103,102,97,105,16.8,17.5,22.1,26.3,21.1,21.4,22.3,19,22.3,23,25.4,22.8,49668,0,0,0,10.091,843
03/23/12,17:45:26,110,110,110,110,109,110,105,112,17.1,17.8,23,28.2,22.1,22.5,23,19,22.3,23.2,25.8,22.8,47544,0,0,0,10.091,790
03/23/12,17:50:26,118,118,118,118,117,118,113,120,16.8,18,24,30.5,23.2,23.7,24,19.5,22.5,23.5,26.8,23,45141,0,0,0,10.091,807
03/23/12,17:55:26,125,126,126,126,126,125,121,128,16.8,18,24.9,32.9,24.4,24.7,25.1,19.7,22.5,23.7,27.5,23,45407,0,0,0,10.091,813
03/23/12,18:00:26,133,133,133,133,134,133,128,135,17.1,18.3,26.1,35.2,26.1,26.1,26.1,20,22.3,24,28,23.2,46939,0,0,0,10.091,820
希望再文本框中输出同样无逗号的相同行、列的内容
$Date $Time PV1 PV2 PV3 PV11 PV12 PV13 PV4 SP T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 YOUYA VZ1 V2 V3 V4 V5
03/23/12 17:40:26 102 102 102 102 103 102 97 105 16.8 17.5 22.1 26.3 21.1 21.4 22.3 19 22.3 23 25.4 22.8 49668 0 0 0 10.091 843
03/23/12 17:45:26 110 110 110 110 109 110 105 112 17.1 17.8 23 28.2 22.1 22.5 23 19 22.3 23.2 25.8 22.8 47544 0 0 0 10.091 790
03/23/12 17:50:26 118 118 118 118 117 118 113 120 16.8 18 24 30.5 23.2 23.7 24 19.5 22.5 23.5 26.8 23 45141 0 0 0 10.091 807
03/23/12 17:55:26 125 126 126 126 126 125 121 128 16.8 18 24.9 32.9 24.4 24.7 25.1 19.7 22.5 23.7 27.5 23 45407 0 0 0 10.091 813
03/23/12 18:00:26 133 133 133 133 134 133 128 135 17.1 18.3 26.1 35.2 26.1 26.1 26.1 20 22.3 24 28 23.2 46939 0 0 0 10.091 820
我变得程序
Private Sub Command6_Click()
Dim TextLine
Dim TextString
Dim SplitStr As Variant
Dim SplitStr2 As Variant
Dim i As Long, j As Long
Dim s As String
Dim m, n As Integer
Dim a() As String
Open "DATAFILE.CSV" For Input As #1 ' 打开文件。
Do While Not EOF(1) ' 循环至文件尾。
Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
Debug.Print TextLine ' 在调试窗口中显示数据。
If TextString = "" Then
TextString = TextLine
Else
TextString = TextString & ";" & TextLine
End If
Loop
Close #1 ' 关闭文件。
SplitStr = Split(TextString, ";")
m = UBound(SplitStr)
For i = 0 To m
SplitStr2 = Split(SplitStr(i), ",")
n = UBound(SplitStr2)
ReDim a(0 To m, 0 To n)
For j = 0 To n
a(i, j) = SplitStr2(j)
Text1.Text = Text1.Text + a(i, j) & " "
Next j
Text1.Text = Text1.Text & vbCrLf
Next i
End Sub
如果csv文件内容过多,运行特别慢甚至会死机 Open "DATAFILE.CSV" For Input As #1
Text1 = Replace(Input(LOF(1), 1), ",", " ")
Close #1 这位版主你太厉害了,如果这样:csv文件为下面这个(一个小的为例,实际会有28列,很多行),
Date,$Time,PV1,PV2,PV3
03/23/12,17:40:26,102,102,102
03/23/12,17:45:26,110,110,110
03/23/12,17:50:26,118,118,118
03/23/12,17:55:26,125,126,126
03/23/12,18:00:26,133,133,133
我做了五个复选框:分别为 Date $Time PV1 PV2 PV3,选中哪个,就只从csv中提取那一列,并从文本框中显示输出?
所以我编的哪个吧csv文件内容给了一个二维数组?应该怎样呢?
十分感谢 同时还想把这个文本框中内容存到EXCEL中去?给定指导吧,我刚接触VB不久
用Split函数切割
比如:
Dim arr() As String
s = "03/23/12,17:40:26,102,102,102"
arr = Split(s, ",")
那么arr(0)就是"03/23/12"
arr(1)就是"17:40:26"
...
结合你的CheckBox判断就可以了。 本帖最后由 bcrun 于 2013-12-01 07:36:22 编辑
补充:VB , 资源