Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET:将CSV文件读入二维数组_Vb.net_Arrays_Csv - Fatal编程技术网

VB.NET:将CSV文件读入二维数组

VB.NET:将CSV文件读入二维数组,vb.net,arrays,csv,Vb.net,Arrays,Csv,我正在从事一个项目,该项目要求我从CSV文件中获取值。我需要对这些值做进一步的处理,如果我能在2D数组中得到这些值,那就太好了。CSV文件的行数和列数会定期更改 我无法在VB.NET/C#中将这些值放入2D数组。能帮我一下吗 下面是我使用的代码: Imports System.IO Public Class Form1 Private Sub ReadCSVFileToArray() Dim strfilename As String Dim num_r

我正在从事一个项目,该项目要求我从CSV文件中获取值。我需要对这些值做进一步的处理,如果我能在2D数组中得到这些值,那就太好了。CSV文件的行数和列数会定期更改

我无法在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

由于列和行的数量经常变化,因此可以使用动态列表而不是固定的二维数组

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

尽管如此,@Mark Gravel的建议是一个比自己做更好的解决方案。

您可以使用将csv(或其他类似数据)轻松方便地读取到数据表(或IDataReader)中。在这种情况下,它始终是我的第一选择-快速且非常健壮。

由于CSV的所有可能变化,解析CSV文件可能非常困难。见:

例如,考虑嵌入引号、逗号等。 因此,阅读行/字符串并拆分它们将不是一件简单的事情

也许你更适合使用

我并不反对马克·格雷维尔,因为你不应该试图重新发明轮子。他的解决方案将表现得最好,并且可能存在许多CSV格式的细微差别,这些细微差别可能会破坏下面演示的简单解析器

话虽如此,您需要一个返回2D数组的CSV解析器。下面的代码就是这样做的(锯齿数组),应该适用于非常简单的CSV文件。这将跳过文件中的标题行

(很抱歉,它不在VB中,但您可以将它放在项目中的帮助器类中)

private string[][]获取数据(字符串文件名)
{
列表数据=新列表();
使用(StreamReader sr=新StreamReader(文件名))
{
bool headerRow=真;
弦线;
而((line=sr.ReadLine())!=null)
{
if(headerRow)
{
headerRow=假;
}
其他的
{
string[]split=line.split(新字符[]{',});
数据。添加(拆分);
}
}
}
返回data.ToArray();
}

Related:,似乎与VS2012不兼容。没有否决票,只是为了获得信息。@Kez以什么方式?什么失败了?你的目标是什么?(IDE可以针对许多框架—net20、net30、net35、net40、net45,还可以针对netcore(“windows应用商店”)、CF、MF、SL、WP7等。
data As New List(Of List(Of String))
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();
}