VB,如何打开并提取一个csv文件中的数据,代码如何编

2024-11-19 09:39:05
推荐回答(1个)
回答1:

Private Sub Command1_Click()
Dim TextLine
Dim TextString
Dim SplitStr As Variant
Dim SplitStr2 As Variant
Dim i As Long, j As Long
Dim s As String

Open App.Path & "\abc.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, ";")

For i = 0 To UBound(SplitStr)
s = ""
SplitStr2 = Split(SplitStr(i), ",")
For j = 0 To UBound(SplitStr2)
s = s & SplitStr2(j)
If j <> UBound(SplitStr2) Then s = s & ","
Next j
MsgBox s
Next i
End Sub