1
我有一个包含多行的excel表格,当我导入我的.csv文件时,它只显示第一行。 我想要的是填满每一行的列表。 cargarCSV()=负载.csv文件 validar()= CSV文件传递到ListViewListView只获取.csv文件的第一行
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());
}
}
}
拆分它然后为每条线添加新行,并用逗号分隔。那是你的问题。但请确保csv不会包含转义逗号 –