private static System.Data.DataTable loadCSVWithHeaderToDataTable(string csvFilePath) { int counter = 0; System.Data.DataTable dataTable = new System.Data.DataTable(); using (ReadWriteCsv.CsvFileReader reader = new ReadWriteCsv.CsvFileReader(csvFilePath)) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); while (reader.ReadRow(row)) { if (counter == 0) { foreach (var column in row) { dataTable.Columns.Add(new System.Data.DataColumn(column, typeof(string))); } } else { System.Data.DataRow dr = dataTable.NewRow(); int index = 0; foreach (var column in row) { dr[index++] = column; } dataTable.Rows.Add(dr); } counter++; } } return(dataTable); }
static void ReadTest(string path) { // Read sample data from CSV file using (ReadWriteCsv.CsvFileReader reader = new ReadWriteCsv.CsvFileReader(path)) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); while (reader.ReadRow(row)) { int q = row.Count; foreach (string s in row) { Console.Write(s); // Console.Write("\t"); } Console.WriteLine(); } } }
private List<Point> ReadShapesFile(String file, List<Point> fullHull) { try { using (ReadWriteCsv.CsvFileReader reader = new ReadWriteCsv.CsvFileReader(file)) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); while (reader.ReadRow(row)) { Point p = new Point(); int i = 0; foreach (string s in row) { if (i == 0) { i++; p.X = Convert.ToInt32(s); } else { i = 0; p.Y = Convert.ToInt32(s); fullHull.Add(p); } } } } } catch (FileNotFoundException) { MessageBox.Show("The temporary files have been moved\nPlease restart the application", "Error! File Not Found!", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (IndexOutOfRangeException) { MessageBox.Show("The temporary files has been modified\nPlease generate a new mesh", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FormatException) { MessageBox.Show("The temporary files has been modified\nPlease generate a new mesh", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } return fullHull; }
private void ReadFile(String file) { try { using (ReadWriteCsv.CsvFileReader reader = new ReadWriteCsv.CsvFileReader(file)) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); while (reader.ReadRow(row)) { int i = 0; int j = 0; foreach (string s in row) { int t = 0; //change this loop to something more robust if (int.TryParse(s, out t)) { //Convert all numbers in csv to ints numbers[i] = Convert.ToInt32(s); i++; } else { //Stores the strings at the end of the file shapes[j] = s; j++; } } } } } catch (FileNotFoundException) { MessageBox.Show("The temporary files have been moved\nPlease restart the application", "Error! File Not Found!", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (IndexOutOfRangeException) { MessageBox.Show("The temporary files has been modified\nPlease generate a new mesh", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FormatException) { MessageBox.Show("The temporary files has been modified\nPlease generate a new mesh", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static DataTable CsvToDataTable(string csvFile, bool colNamesIn1stRow, bool ignoreExtraDataColumnsIfFoundAfterHeaderColumns) { DataTable dt = null; try { dt = new DataTable(); List <string> firstRowDataColumns = new List <string>(); // holder first row data/columnnames //int rowNumber = 0; int colNumber = 0; bool isFirstRow = true; using (ReadWriteCsv.CsvFileReader reader = new ReadWriteCsv.CsvFileReader(csvFile)) { dt = new DataTable(); ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); while (reader.ReadRow(row)) //rows loop { colNumber = 0; DataRow dtRow = null; if (!isFirstRow) { dtRow = dt.NewRow(); } foreach (string s in row) //column loop { if (isFirstRow) { if (colNamesIn1stRow) { dt.Columns.Add(s); } else { dt.Columns.Add("Column" + (colNumber + 1).ToString()); firstRowDataColumns.Add(s); } } else { if (colNumber < dt.Columns.Count) { dtRow[colNumber] = s; } else { if (!ignoreExtraDataColumnsIfFoundAfterHeaderColumns) { Console.WriteLine("Invalid column found (more columns in the datarow than the header of the file!)"); throw new Exception("Invalid column found (more columns in the datarow than the header of the file!)"); } } } colNumber++; } //column loop if (isFirstRow) { isFirstRow = false; if (firstRowDataColumns.Count > 0) { int i = 0; dtRow = dt.NewRow(); foreach (string s in firstRowDataColumns) { dtRow[i] = s; i++; } dt.Rows.Add(dtRow); } } else { dt.Rows.Add(dtRow); } // Console.WriteLine(); } //rows loop } } catch (Exception ex) { Console.WriteLine(ex); dt = null; } return(dt); }