CSV 파일에서 값을 가져와야하는 프로젝트에서 작업하고 있습니다. 이 값으로 추가 처리를해야하며 2D 배열에서이 값을 가질 수 있으면 좋을 것입니다. CSV 파일의 행 및 열 수는 정기적으로 변경됩니다.VB.NET : CSV 파일을 2D 배열로 읽기
VB.NET/C#에서이 값을 2D 배열로 가져올 수 없습니다. 그걸 좀 도와 주실 수 있을까요? 여기
내가 사용하는 코드 :
Imports System.IO
Public Class Form1
Private Sub ReadCSVFileToArray()
Dim strfilename As String
Dim num_rows As Long
Dim num_cols As Long
Dim x As Integer
Dim y As Integer
Dim strarray(1, 1) As String
' Load the file.
strfilename = "test.csv"
'Check if file exist
If File.Exists(strfilename) Then
Dim tmpstream As StreamReader = File.OpenText(strfilename)
Dim strlines() As String
Dim strline() As String
strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)
' Redimension the array.
num_rows = UBound(strlines)
strline = strlines(0).Split(",")
num_cols = UBound(strline)
ReDim strarray(num_rows, num_cols)
' Copy the data into the array.
For x = 0 To num_rows
strline = strlines(x).Split(",")
For y = 0 To num_cols
strarray(x, y) = strline(y)
Next
Next
' Display the data in textbox
For x = 0 To num_rows
For y = 0 To num_cols
TextBox1.Text = TextBox1.Text & strarray(x, y) & ","
Next
TextBox1.Text = TextBox1.Text & Environment.NewLine
Next
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ReadCSVFileToArray()
End Sub
End Class
관련 : http://stackoverflow.com/questions/906841/csv-parser-를 reader-for-c, http://stackoverflow.com/questions/1103495/is-there-a-proper-way-to-read-csv-files, http://stackoverflow.com/questions/3509640 reading-csv-file-c –