【发布时间】:2011-10-06 03:44:43
【问题描述】:
ArrayList fileList = new ArrayList();
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName);
while ((line = file.ReadLine()) != null)
{
// Puts elements in table
fileList.Add(line.Split(';'));
}
file.Close();
}
for (int i = 0; i < fileList.Count; i++)
{
for (int x = 0; x < (fileList[i] as string[]).Length; x++)
{
// if (x ==0)
// {
//fileList[0] must Be int
// }
// if (x==1)
//fileList[1] must be string
this.textBox2.Text += ((fileList[i] as string[])[x] + " ");
}
this.textBox2.Text += Environment.NewLine;
}
}
我到这里为止。
我从 CSV 文件中获取元素。
我现在需要确保 1 列只有数字整数 (1,2,3,4,5),第二个 列只有名称(因此它将具有字符串或字符类型),第三个姓氏等。
行显示如下:1;George;Mano;
如何确定 CSV 文件的类型正确?
我认为有关此问题的更多代码将放在 2 for 语句中。
非常感谢,
乔治。
【问题讨论】:
-
您的帖子表明您的行类似于
1;2;3;、johnson;smith;gower、john;james;susan。这是正确的,还是他们是这样的:1;johnson;john,2;smith;james,3;gower;susan -
你没有问这个,所以我只是发表评论。为什么要重新发明轮子?有几个开源 csv 库。我使用 linq2csv,您可以将其与 DataAnnotation 属性一起使用以进行验证。看到这个链接stackoverflow.com/questions/1405038/reading-csv-files-in-net
-
@Agent-j - 我相信他最初的问题是说第一列(第一个值)是一个整数。
标签: c# csv arraylist validation