C#中csv格式转换为datatable,我现在想要从firstname字段开始读取,需要怎么操作
public static ExecuteResult CsvToDatatable(string pathname,string firstname)
{
ExecuteResult ER = new ExecuteResult();
int n = 0;//跳过的行数
DataTable dt = new DataTable();
String csvSplitBy = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
StreamReader reader = new StreamReader(pathname, Encoding.Default, false);
int i = 0, m = 0;
reader.Peek();
while (reader.Peek() > 0)
{
m = m + 1;
string str = reader.ReadLine();
if (m >= n + 1)
{
if (m == n + 1) //如果是字段行,则自动加入字段。
{
MatchCollection mcs = Regex.Matches(str, csvSplitBy);
foreach (Match mc in mcs)
{
dt.Columns.Add(mc.Value); //增加列标题
}
}
else
{
MatchCollection mcs = Regex.Matches(str, "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)");
i = 0;
System.Data.DataRow dr = dt.NewRow();
foreach (Match mc in mcs)
{
dr[i] = mc.Value;
i++;
}
dt.Rows.Add(dr); //DataTable 增加一行
}
}
}