Чтение csv-данных, а затем сохранение в таблице данных с помощью C#
это мой формат csv файла:
Дата, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, Currency_Name, 24 октября 2016 года, 1.0891, 113.29, 1.9558, 27.021, 7.4394, 0.89015, 308.53, 4.3166, 4.5065, 9.7050, 1.0821, 8.9855, 7.5083, 1.450.7970 3.4200, 1.4547, 7.3775, 8.4478, 14178.99, 72.7975, 1234.29, 20.2263, 4.5444, 1.5215, 52.562, 1.5159, 38.119, 15.1167, 4.2057
Я хочу обратиться вот так
Date Currency Rate 24/10/2016 Currency_Name 0.3514 24/10/2016 Currency_Name 0.0843 24/10/2016 Currency_Name 0.7403 24/10/2016 Currency_Name 3.430
Что я уже пробовал:
private static DataTable getCSVData(string csv_file_path) { String[] values = File.ReadAllText(csv_file_path).Split(','); DataTable dt = new DataTable(); dt.Columns.Add("Date"); dt.Columns.Add("Currency"); dt.Columns.Add("Rate"); //foreach (string i in values) //{ // DataRow row = dt.NewRow(); // row["Date"] = values[32]; // dt.Rows.Add(row); //} for(int i =0;i<=0;i++) { DataRow row = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[1]; row["Rate"] = values[33]; // dt.Rows.Add(row); // DataRow row1 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[2]; row["Rate"] = values[34]; // dt.Rows.Add(row); //DataRow row2 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[3]; row["Rate"] = values[35]; // dt.Rows.Add(row); //DataRow row3 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[4]; row["Rate"] = values[36]; // dt.Rows.Add(row); //DataRow row4 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[5]; row["Rate"] = values[37]; // dt.Rows.Add(row); //DataRow row5 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[6]; row["Rate"] = values[38]; // dt.Rows.Add(row); //DataRow row6 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[7]; row["Rate"] = values[39]; //dt.Rows.Add(row); //DataRow row7 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[8]; row["Rate"] = values[40]; //dt.Rows.Add(row); //DataRow row8 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[9]; row["Rate"] = values[41]; //dt.Rows.Add(row); //DataRow row9 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[10]; row["Rate"] = values[42]; //dt.Rows.Add(row); //DataRow row10 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[11]; row["Rate"] = values[43]; //dt.Rows.Add(row); //DataRow row11 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[12]; row["Rate"] = values[44]; //dt.Rows.Add(row); //DataRow row12 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[13]; row["Rate"] = values[45]; // dt.Rows.Add(row); //DataRow row13 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[14]; row["Rate"] = values[46]; // dt.Rows.Add(row); //DataRow row14 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[15]; row["Rate"] = values[47]; // dt.Rows.Add(row); //DataRow row15 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[16]; row["Rate"] = values[48]; // dt.Rows.Add(row); //DataRow row16 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[17]; row["Rate"] = values[49]; // dt.Rows.Add(row); //DataRow row17 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[18]; row["Rate"] = values[50]; // dt.Rows.Add(row); //DataRow row18 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[19]; row["Rate"] = values[51]; // dt.Rows.Add(row); //DataRow row19 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[20]; row["Rate"] = values[52]; // dt.Rows.Add(row); //DataRow row20 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[21]; row["Rate"] = values[53]; // dt.Rows.Add(row); //DataRow row21 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[22]; row["Rate"] = values[54]; // dt.Rows.Add(row); //DataRow row22 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[23]; row["Rate"] = values[55]; //dt.Rows.Add(row); //DataRow row23 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[24]; row["Rate"] = values[56]; // dt.Rows.Add(row); //DataRow row24 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[25]; row["Rate"] = values[57]; // dt.Rows.Add(row); //DataRow row25 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[26]; row["Rate"] = values[58]; //dt.Rows.Add(row); //DataRow row26 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[27]; row["Rate"] = values[59]; // dt.Rows.Add(row); // DataRow row27 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[28]; row["Rate"] = values[60]; //dt.Rows.Add(row); //DataRow row28 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[29]; row["Rate"] = values[61]; //dt.Rows.Add(row); //DataRow row29 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[30]; row["Rate"] = values[62]; //dt.Rows.Add(row); //DataRow row30 = dt.NewRow(); row["Date"] = Convert.ToDateTime(values[32]).ToString("dd/MM/yyyy"); row["Currency"] = values[31]; row["Rate"] = values[63]; dt.Rows.Add(row); //foreach (string d in values) //{ // // var names = d["fullname"].ToString().Split(' '); // dt.Rows[0]["date"] = values[32]; // dt.Rows.Add(d); //} } return dt; }
Patrice T
Znd у вас есть вопрос ? или проблема ?