Home > Software engineering >  Vb to read CSV
Vb to read CSV

Time:10-09

Why this way, while reading large CSV data output text box particularly slow
Private Sub Command6_Click ()
Dim TextLine
Dim TextString
Dim SplitStr As the Variant
Dim SplitStr2 As the Variant
Dim As Long, I j As Long
Dim s As String
Dim m, n As Integer
Dim (a) As String
Open "DATAFILE. CSV" For Input As # 1 'Open the file,
The Do While Not EOF (1) 'circular end of the file to,
Line Input # 1, the TextLine 'read in a row and give it a variable,

If TextString="" Then
TextString=TextLine
The Else
TextString=TextString & amp; ";" & TextLine
End the If
Loop
Close # 1 'Close the file,

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) & amp; "
"Next j
Text1. Text=Text1. Text & amp; VbCrLf
Next I
End Sub

CodePudding user response:

I already say with you,

With a variable to hold the first and the last one-time setup to text boxes, didn't you see?

CodePudding user response:

I already say with you,

With a variable to hold the first and the last one-time setup to text boxes, didn't you see?
Oh, oh I see,
Both dim STR dim I as long as the string, r both dim the data as string () as string, lines () as string open "XXX. CSV" for input as # 1 STR=input (lof (1), (1) the close # 1 lines=split (STR, VBCRLF) for I=lbound (lines) to ubound (lines) data=https://bbs.csdn.net/topics/split (lines (I), ", ") if I=lbound (lines), then r=data (0) & amp; "" & amp; Data (1) & amp; "" & amp; Data (3) & amp; "" & amp; Data (4) the else r=r & amp; VBCRLF & amp; Data (0) & amp; "" & amp; Data (1) & amp; "" & amp; Data (3) & amp; "" & amp; Data (4) end if next text1. Text=r


But always prompted to lower the standard, influential CSV data quantity is big??

CodePudding user response:

refer to the second floor heianzhiwang response:
I already say with you,

With a variable to hold the first and the last one-time setup to text boxes, didn't you see?
Oh, oh I see,
Both dim STR dim I as long as the string, r both dim the data as string () as string, lines () as string open "XXX. CSV" for input as # 1 STR=input (lof (1), (1) the close # 1 lines=split (STR, VBCRLF) for I=lbound (lines) to ubound (lines) data=https://bbs.csdn.net/topics/split (lines (I), ", ") if I=lbound (lines), then r=data (0) & amp; "" & amp; Data (1) & amp; "" & amp; Data (3) & amp; "" & amp; Data (4) the else r=r & amp; VBCRLF & amp; Data (0) & amp; "" & amp; Data (1) & amp; "" & amp; Data (3) & amp; "" & amp; Data (4) end if next text1. Text=r


But always prompted to lower the standard, influential CSV data quantity is big??

You are debugging, you choose in the right column,

CodePudding user response:

Ok, very thank you for your guidance, I try

I do such a thing: CSV file save generated from intouch, so every time the data is not the same, but always 28 column column, it is not the number of rows, the 28 column of the top two listed as date, time, behind 26 column is a parameter list
In vb, made up of 26 checkbox in the corresponding CSV 26 parameters, which check box is selected, the corresponding will read CSV and columns in the text box shows the parameters, the last to save these data into excel,

CodePudding user response:

 Open "DATAFILE. CSV" For Input As # 1 'Open the file, 
The Do While Not EOF (1) 'circular end of the file to,
Line Input # 1, the TextLine 'read in a row and give it a variable,

If TextString="" Then
TextString=TextLine
The Else
TextString=TextString & amp; ";" & TextLine
End the If
Loop
Close # 1 'Close the file,

SplitStr=Split (TextString, ";" )
M=UBound (SplitStr)
TextString=""
For I=0 To m - 1
SplitStr2=Split (SplitStr (I), ", ")
N=UBound (SplitStr2)
For j=0 To n - 1
If Check1 (j) Then
TextString=TextString. Text & amp; SplitStr2 (j) & amp; "
"End the If
Next j
TextString=TextString & amp; VbCrLf
Next I
Text1=TextString

CodePudding user response:

Send a few posts, all of this problem,

CodePudding user response:

I don't quite understand is that the building Lord: why do you want to connect the text line by line, split again,

When a file is very long, the connection string system will redistribute variable cache, this process can be slow,

If I do, will each input line, was assigned to display controls, string is the slowest operation, to avoid unnecessary string manipulation,
 Open "DATAFILE. CSV" For Input As # 1 'Open the file, 
The Do While Not EOF (1) 'circular end of the file to,
Line Input # 1, the TextLine 'read in a row and give it a variable,
TextLine=Replace (TextLine, ", ", "")
Text1=Text1 & amp; TextLine & amp; VbCrlf
Loop
Close # 1 'Close the file,

CodePudding user response:

I don't quite understand is that the building Lord: why do you want to connect the text line by line, split again,

When a file is very long, the connection string system will redistribute variable cache, this process can be slow,

If I do, will each input line, was assigned to display controls, string is the slowest operation, to avoid unnecessary string manipulation,

Because each row of data will be fixed in which one is not needed, thus completely write all the data is entered

CodePudding user response:

That doesn't need to put all the lines together ah,

Every line break up soon?

CodePudding user response:

See another post back to the code for you,

Try to avoid a long string of connection repeatedly,

CodePudding user response:

In a loop directly manipulate Text display control, is also very slow,

Should be in a loop, each reading a line, just break up content, and the need to the content of the extracted, using String type variable connection and save,
This operation is completed, a one-time assigned to Text controls,

CodePudding user response:

Very thanks for your guidance, I have a try,

CodePudding user response:

Look from your program is to put, replace the blank space? If it is not necessary to think you so epic!
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related