public static void UpdateTable(CsvTable table) { FileStream fcreate = File.Open(table.Path, FileMode.Create); try { using (StreamWriter sw = new StreamWriter(fcreate)) { foreach (Row row in table.Rows) { string newLine = ""; foreach (Cell cell in row.Cells) { newLine += cell.Text + ","; } newLine = newLine.TrimEnd(','); sw.WriteLine(newLine); } sw.Flush(); sw.Close(); } } catch (IOException e) //catch a specific type of Exception { Console.WriteLine("Error reading the file"); Console.WriteLine(e.Message); } }
public static void OutputTable(CsvTable table) { bool colorStatus = false; //Graphics.AlternateGreenAndBlack(); //for (int i = 0; i <= table.MaxRowLength; i++) //{ // Console.Write(PadBoth(i.ToString())); // Console.Write(" |"); //} Graphics.AlternateGreenAndBlack(); for (int i = 0; i < table.Rows.Count; i++) // Row row in table.Rows) { Console.WriteLine(); Console.Write(PadBoth((i + 1).ToString())); Console.Write(" |"); foreach (Cell cell in table.Rows[i].Cells) { Console.Write(PadBoth(cell.Text)); Console.Write(" |"); } colorStatus = !colorStatus; Graphics.AlternateGreenAndBlack(); } }
public static void GetTableFixed() { CsvTable table = new CsvTable(); table.Path = GetPath(); var rows = new List <Row>(); table.Rows = rows; StreamReader sr = new StreamReader(table.Path); using (var csv = new CsvReader(sr)) { //csv.Read(); //csv.ReadHeader(); while (csv.Read()) { Console.WriteLine(); for (int i = 0; i < 10; i++) { string foo = csv.GetField(i); Console.Write(foo); Console.Write("|"); } } Console.ReadLine(); } }
public static void DeleteColumn(CsvTable table, int colNumber) { foreach (Row row in table.Rows) { row.Cells.RemoveAt(colNumber - 1); } UpdateTable(table); }
public static CsvTable GetTable(string fileName) { CsvTable table = new CsvTable(); table.Path = GetPath(fileName); var rows = new List <Row>(); table.Rows = rows; try { using (StreamReader sr = new StreamReader(table.Path)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] words = line.Split(','); Row row = new Row(); var cells = new List <Cell>(); row.Cells = cells; foreach (string word in words) { Cell cell = new Cell(); cell.Text = word; row.Cells.Add(cell); } table.Rows.Add(row); } sr.Close(); } } catch (IOException e) { Console.WriteLine("Error reading the file"); Console.WriteLine(e.Message); } foreach (Row row in table.Rows) { while (row.Cells.Count < table.MaxRowLength) { var cell = new Cell(); cell.Text = "-"; row.Cells.Add(cell); } } return(table); }
public static void WriteCell(CsvTable table, string newWord) { FileStream fcreate = File.Open(table.Path, FileMode.Append); try { using (StreamWriter sw = new StreamWriter(fcreate)) { sw.WriteLine("{0},{0}", newWord); } } catch (IOException e) //catch a specific type of Exception { Console.WriteLine("Error reading the file"); Console.WriteLine(e.Message); } }
public static void AddColumn(CsvTable table, string usrInput) { string[] words = usrInput.Split(','); for (int i = 0; i < words.Length; i++) { if (i + 1 > table.Rows.Count) { //skip } else { var cell = new Cell(); cell.Text = words[i]; table.Rows[i].Cells.Add(cell); } } UpdateTable(table); }
public static void WipeFile(CsvTable table) { FileStream fcreate = File.Open(table.Path, FileMode.Create); // will create the file or overwrite it if it already exists try { using (StreamWriter sw = new StreamWriter(fcreate)) { sw.WriteLine("{0}", ""); sw.Flush(); sw.Close(); } } catch (IOException e) //catch a specific type of Exception { Console.WriteLine("Error reading the file"); Console.WriteLine(e.Message); } }
public static void AddRow(CsvTable table, string usrInput) { string[] words = usrInput.Split(','); FileStream fappend = File.Open(table.Path, FileMode.Append); // will create the file or overwrite it if it already exists try { using (StreamWriter sw = new StreamWriter(fappend)) { sw.WriteLine(string.Join(',', words)); sw.Flush(); sw.Close(); } } catch (IOException e) //catch a specific type of Exception { Console.WriteLine("Error reading the file"); Console.WriteLine(e.Message); } }
public static void DeleteRow(CsvTable table, int rowNumber) { table.Rows.RemoveAt(rowNumber - 1); UpdateTable(table); }
public void Start() { string initialInput = null; do { Graphics.Initialize(); Console.WriteLine("Enter the file name:"); initialInput = Console.ReadLine(); CsvTable wordsCSV = ReadingCSVFiles.GetTable(initialInput); Console.WriteLine(wordsCSV.Path); ReadingCSVFiles.OutputTable(wordsCSV); string usrAction = null; Console.ReadLine(); do { Graphics.Red(); Console.WriteLine("(1) add row, (2) delete a row, (3) add a column, (4) delete a column, (5) read csvhelper (6) write csvhelper (7)convert to uppercase (0) exit"); Graphics.Black(); usrAction = Console.ReadLine(); if (usrAction == "1") { Console.WriteLine("Type input separated by commas:"); string usrInput = Console.ReadLine(); WritingCSVFiles.AddRow(wordsCSV, usrInput); } else if (usrAction == "2") { Console.WriteLine("Enter the number of the row to delete"); WritingCSVFiles.DeleteRow(wordsCSV, Int32.Parse(Console.ReadLine())); } else if (usrAction == "3") { Console.WriteLine("Type input separated by commas:"); string usrInput = Console.ReadLine(); WritingCSVFiles.AddColumn(wordsCSV, usrInput); } else if (usrAction == "4") { Console.WriteLine("Enter the number of the column to delete"); WritingCSVFiles.DeleteColumn(wordsCSV, Int32.Parse(Console.ReadLine())); } else if (usrAction == "5") { ReadingCSVFiles.CSVHelperTest(); } else if (usrAction == "6") { WritingCSVFiles.WriteCSVHelper(); } else if (usrAction == "7") { WritingCSVFiles.CsvHelperToUpper(); } Console.ReadLine(); wordsCSV = ReadingCSVFiles.GetTable(initialInput); ReadingCSVFiles.OutputTable(wordsCSV); Console.ReadLine(); } while (usrAction != "0"); } while (initialInput != "0"); }