C#用文件流读取cvs内容并返回DataTable,并把第一行设为列名
public static bool OpenCSVFile(ref DataTable mycsvdt, string filepath) { string strpath = filepath; int intColCount = 0; bool blnFlag = true; DataColumn mydc; DataRow mydr; string strline; string[] aryline; System.IO.StreamReader mysr = new System.IO.StreamReader(strpath, System.Text.Encoding.Default); while ((strline = mysr.ReadLine()) != null) { aryline = strline.Split(new char[] { ',' }); if (blnFlag) { blnFlag = false; intColCount = aryline.Length; int col = 0; for (int i = 0; i < aryline.Length; i++) { col = i + 1; mydc = new DataColumn(aryline[i]); mycsvdt.Columns.Add(mydc); } } mydr = mycsvdt.NewRow(); for (int i = 0; i < intColCount; i++) { mydr[i] = aryline[i]; } mycsvdt.Rows.Add(mydr); } return true; } }