读取csv 文件

Posted skywss27

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取csv 文件相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadCSV
{
class Program
{
public static void Main(string[] args)
{
List<Contract> list = ReadCSV(@"?C:UsersAdministratorSourceReposReadCSVReadCSVinDebugEE4.csv");

}
public static List<Contract> ReadCSV( string path)
{

List<Contract> list = new List<Contract>();
StreamReader sr = new StreamReader("EE6.csv", Encoding.UTF8);
String line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("DisplayName"))
continue;
list.Add(readlineToContract(line, 15));
}
return list;
}

public static Contract readlineToContract(string line, int count)
{
Contract contract = new Contract();
string[] strArray = line.Split(‘,‘);
if (count == strArray.Count())
{
contract.Name = strArray[4];
contract.DisplayName = strArray[5];
contract.Type = strArray[8];
contract.Category = strArray[9];
contract.State = strArray[10];
contract.Stage = strArray[11];
}
else
{
//deal with doublue " contains ,
List<string> temp = new List<string>();
for (int i = 0; i < line.ToArray().Length; i++)
{
if (line[i] != ‘"‘)
{
int index = line.IndexOf(",", i);
if (index == -1)
temp.Add(line.Substring(i));
else
{
temp.Add(line.Substring(i, index - i));
i = index;
}
}
else if (line[i] == ‘,‘)
{
temp.Add(string.Empty);
}
else if (line[i] == ‘"‘)
{
int index = line.IndexOf(""", i+1);
temp.Add(line.Substring(i+1, index - i-1));
i = index;
}

}
if (line.EndsWith(",")) temp.Add(string.Empty);

contract.Name = temp[4];
contract.DisplayName = temp[5];
contract.Type = temp[8];
contract.Category = temp[9];
contract.State = temp[10];
contract.Stage = temp[11];
}
return contract;
}
}

class Contract
{
public string Name { set; get; }
public string DisplayName { set; get; }
public string Type { set; get; }
public string Category { set; get; }
public string State { set; get; }
public string Stage { set; get; }


}
}














































































以上是关于读取csv 文件的主要内容,如果未能解决你的问题,请参考以下文章

ruby读取csv文件数据

vb如何实时读取csv文件?

如何读取CSV文件后合并内容

Qt中如何读取csv文件到二维数组

python 读取多个csv文件中某一列,并生成一个新csv文件

CSV 文件读取操作