2011-04-25 87 views
2

我正在處理一個需要我從CSV文件中獲取值的項目。我必須用這些值做進一步的處理,如果我可以在2D數組中使用這些值,那將會很棒。 CSV文件的行數和列數會定期更改。VB.NET:將CSV文件讀取到2D數組中

我無法將這些值轉換爲VB.NET/C#中的二維數組。我可以請幫忙嗎?

在這裏,我使用的代碼:

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 
+0

相關:http://stackoverflow.com/questions/906841/csv-parser-閱讀器爲C, http://stackoverflow.com/questions/1103495/is-there-a-proper-way-to-read-csv-files, http://stackoverflow.com/questions/3509640/ reading-csv-file-c – 2011-04-25 09:40:13

回答

0

由於行和列的數量經常變化,您可以使用動態列表,而不是一個固定的二維數組。

List<List<string>> data = new List<List<string>>(); 

然後當你解析的CSV文件可以在上述結構中動態地建立數據,添加新List<string>data在CSV文件中的每一新行。

更新:的VB.NET相當於是一樣的東西

data As New List(Of List(Of String)) 

話雖這麼說,@馬克Gravell的建議是不是這樣做你自己一個更好的解決方案。

2

您可以使用CsvReader from here輕鬆方便地將csv(或其他類似數據)讀取到DataTable(或IDataReader)中。對於這種情況,它總是我的第一選擇 - 快速且相當健壯。

+0

看起來與VS2012不兼容。沒有downvote,僅供參考。 – Kez 2013-05-31 12:54:42

+0

@Kez以什麼方式?什麼失敗?你的目標是什麼框架? (IDE可以針對許多框架 - net20,net30,net35,net40,net45,還有netcore(「windows store」),CF,MF,SL,WP7等。 – 2013-05-31 13:00:59

0

我不與Marc Gravell不同意,因爲你不應該試圖重新發明輪子更好。他的解決方案表現最好,可以有許多CSV格式的細微差別,可以搞砸一個簡單的解析器,如下面演示的那樣。

這就是說,你要求一個返回二維數組的CSV解析器。下面的代碼只是(鋸齒陣列),並應該爲一個非常簡單的CSV文件。這會跳過文件中的標題行。

(抱歉,這不是在VB,但你可以把它放在一個輔助類在你的項目)

private string[][] GetData(string fileName) 
{ 
    List<string[]> data = new List<string[]>(); 

    using (StreamReader sr = new StreamReader(fileName)) 
    { 
    bool headerRow = true; 
    string line; 

    while ((line = sr.ReadLine()) != null) 
    { 
     if (headerRow) 
     { 
     headerRow = false; 
     } 
     else 
     { 
     string[] split = line.Split(new char[] { ',' }); 

     data.Add(split); 
     } 
    } 
    } 

    return data.ToArray(); 
}