ListView只获取.csv文件的第一行

时间:2016-04-23 09:05:13

标签: c# listview csv

我有一个包含多行的excel表,当我导入我的.csv文件时,它只显示第一行。 我想要的是每行填充列表。 cargarCSV()=加载.csv文件 validar()=将csv文件传递给listview

        private void cargarCSV() //Load .csv
    {
        OpenFileDialog dialogoCargar = new OpenFileDialog();
        dialogoCargar.Filter = "Archivos CSV|*.csv";
        dialogoCargar.FilterIndex = 1;
        if(dialogoCargar.ShowDialog() == DialogResult.OK)
        {
            filepath = dialogoCargar.FileName;
            txtbox_ArchivoCargado.Text = filepath;
        }
    }

        private void Validar() //Pass .csv to ListView
    {
        empleadosValido = true;
        try {
            FileStream fileStreamNew = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite);
            StreamReader streamRead = new StreamReader(fileStreamNew);
            string strView = streamRead.ReadToEnd();
            streamRead.Close();
            fileStreamNew.Close();
            String[] strArray = strView.Split(new char[] { ',' });
            ListViewItem item = new ListViewItem(strArray[0].ToString());

            item.SubItems.Add(strArray[1].ToString());
            item.SubItems.Add(strArray[2].ToString());
            item.SubItems.Add(strArray[3].ToString());
            item.SubItems.Add(strArray[4].ToString());
            item.SubItems.Add(strArray[5].ToString());
            item.SubItems.Add(strArray[6].ToString());

            list_Previ.Items.Add(item);
             }catch (Exception ex)
        {
            if (ex is IOException)
            {
                MessageBox.Show("El archivo se encuentra en uso por otro                    programa\nPor favor cierra otros programas e intenta de nuevo.", "Corporativo  Acosta | Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                empleadosError = true;
                MessageBox.Show(ex.ToString());
            }
            if (ex is IndexOutOfRangeException)
            {
                MessageBox.Show("Hay un problema con tus columnas.\nVerifica que correspondan las columnas a importar\ncon las de la tabla (7 columnas)", "Corporativo Acosta | Error", MessageBoxButtons.OK ,MessageBoxIcon.Error);
                empleadosError = true;
                MessageBox.Show(ex.ToString());
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

在不改变实际代码的情况下,但为文件中的每一行插入一个循环,你应该去

foreach(string strView = File.ReadLines(filepath))
{
    String[] strArray = strView.Split(new char[] { ',' });
    ListViewItem item = new ListViewItem(strArray[0].ToString());

    item.SubItems.Add(strArray[1].ToString());
    item.SubItems.Add(strArray[2].ToString());
    item.SubItems.Add(strArray[3].ToString());
    item.SubItems.Add(strArray[4].ToString());
    item.SubItems.Add(strArray[5].ToString());
    item.SubItems.Add(strArray[6].ToString());
    list_Previ.Items.Add(item);
}

当然,您可以删除对FileStream和StreamReader变量的所有引用。

另外,如果您预测某些行包含少于7个元素,我建议在将该元素添加到ListView Items集合之前添加对数组长度的检查,并且不依赖于异常处理来继续。使用异常来驱动代码是一种不好的做法,在性能方面遇到异常代价很高(比在添加元素之前添加if更多),所以应考虑这样的事情

if(strArray.Length > 1) item.SubItems.Add(strArray[1].ToString());
if(strArray.Length > 2) item.SubItems.Add(strArray[2].ToString());
....