更新时间:2023-09-17 23:41:58
在一开始就添加另一个ReadLine
;它将跳过第一行。当你使用Split
的结果时,只使用索引0处结果数组的元素。这就是全部。
更好,因为你的文件很小,而不是使用String.Reader
,使用System.IO.File.ReadAllLines
。它将返回行数组,因此您可以忽略索引为0的行。请参阅:
http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx [ ^ ]。-SA
检查以下链接。
File.ReadAllLines方法示例
为什么要读取CSV文件使用Reader [ ^ ]如果可以使用 OLEDB [ ^ ] to 导入数据 [ ^ ]
Hi,
Below is my CSV content
Number of managed system,
80, compliance
7, non_colpliance
I need only these values (80 and 7). I need to skip the rest of the data
Below is my code
StreamReader sr = new StreamReader(filepath);
string line = sr.ReadLine();
string[] value = line.Split(',');
DataTable dt = new DataTable();
DataRow row;
foreach (string dc in value)
{
dt.Columns.Add(new DataColumn(dc));
}
while ( !sr.EndOfStream )
{
value = sr.ReadLine().Split(',');
if(value.Length == dt.Columns.Count)
{
row = dt.NewRow();
row.ItemArray = value;
dt.Rows.Add(row);
}
}
Please help me to achive this.
Thanks in advance.
Add anotherReadLine
in the very beginning; it will skip first line. When you use result ofSplit
, use only the element of the resulting array at index 0. That's all.
Better yet, as your file is small, instead of usingString.Reader
, useSystem.IO.File.ReadAllLines
. It will return you the array of lines, so you can ignore the line with the index 0. Please see:
http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx[^].—SA
Check following link.
File.ReadAllLines Method with example
Why to read CSV file using Reader[^] if it possible to use OLEDB[^] to import data[^]?