public static string SaveLastPrintedTableToCSV(string filePath, DataTable dataTable) { bool exists = System.IO.Directory.Exists(filePath); // using (System.IO.StreamWriter sr = new System.IO.StreamWriter(filePath, true, Encoding.UTF8)) using (ReadWriteCsv.CsvFileWriter writer = new ReadWriteCsv.CsvFileWriter(filePath)) { ReadWriteCsv.CsvRow header = new ReadWriteCsv.CsvRow(); foreach (System.Data.DataColumn dataColumn in dataTable.Columns) { header.Add(dataColumn.ColumnName); } writer.WriteRow(header); foreach (System.Data.DataRow dataRow in dataTable.Rows) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); foreach (System.Data.DataColumn dataColumn in dataRow.Table.Columns) { row.Add(dataRow[dataColumn.ColumnName].ToString()); } writer.WriteRow(row); } } return(filePath); }
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(); } } }
public static bool DataTableToCSV(DataTable dt, string csvFile, bool writeColumnNames) { bool r = true; try { // Write sample data to CSV file using (ReadWriteCsv.CsvFileWriter writer = new ReadWriteCsv.CsvFileWriter(csvFile)) { //writing columnNames if (writeColumnNames) { ReadWriteCsv.CsvRow rowCols = new ReadWriteCsv.CsvRow(); foreach (DataColumn c in dt.Columns) { rowCols.Add(c.ColumnName); } writer.WriteRow(rowCols); } foreach (DataRow dr in dt.Rows) //writing data. { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); for (int j = 0; j < dt.Columns.Count; j++) { row.Add(dr[j].ToString()); } writer.WriteRow(row); } } } catch (Exception ex) { Console.WriteLine(ex); r = false; } return(r); }
/// <summary> /// I'm not exactly sure the use of this?? /// All it does is create create a file /// </summary> /// <param name="size"></param> public void RandomShape(Sizes size) { Random ran = new Random(); int Max_Height = size.Window_Height; int max_Width = size.Window_Width; int offset = size.OFFSET; using (ReadWriteCsv.CsvFileWriter writer = new ReadWriteCsv.CsvFileWriter("Shape.csv")) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); int numberOfShapes = ran.Next(15); for (int i = 0; i < numberOfShapes; i++) { row.Add(ran.Next(max_Width).ToString()); row.Add(ran.Next(Max_Height).ToString()); writer.WriteRow(row); row.Clear(); } } }
public ReadWriteCsv.CsvRow ToCsv() { ReadWriteCsv.CsvRow l = new ReadWriteCsv.CsvRow(); l.Add(this.KYCRSP_FUNDNO.ToString()); if(this.MCALDT == null || this.MCALDT.Year < 1900) l.Add(""); else l.Add(this.MCALDT.ToString("dd/MM/yyyy")); l.Add(this.FMRET); l.Add(this.FMTNA); l.Add(this.RmRf); l.Add(this.RmRfd); l.Add(this.SMB); l.Add(this.HML); l.Add(this.Mom); l.Add(this.RF); l.Add(this.Rfd); l.Add(this.FLOW); l.Add(this.FRONT_LOAD); if(this.Fflbegdt == null || this.Fflbegdt.Year < 1900) l.Add(""); else l.Add(this.Fflbegdt.ToString("dd/MM/yyyy")); if(this.Fflenddt == null || this.Fflenddt.Year < 1900) l.Add(""); else l.Add(this.Fflenddt.ToString("dd/MM/yyyy")); l.Add(this.REAR_LOAD); if(this.Frlbegdt == null || this.Frlbegdt.Year < 1900) l.Add(""); else l.Add(this.Frlbegdt.ToString("dd/MM/yyyy")); if(this.Frlenddt == null || this.Frlenddt.Year < 1900) l.Add(""); else l.Add(this.Frlenddt.ToString("dd/MM/yyyy")); l.Add(this.FEXP_RATIO); l.Add(this.FMGMT_FEE); l.Add(this.FTURN_RATIO); if(this.Ffebegdt == null || this.Ffebegdt.Year < 1900) l.Add(""); else l.Add(this.Ffebegdt.ToString("dd/MM/yyyy")); if(this.Ffeenddt == null || this.Ffeenddt.Year < 1900) l.Add(""); else l.Add(this.Ffeenddt.ToString("dd/MM/yyyy")); l.Add(this.TotalLoads.ToString()); l.Add(this.r_rf.ToString()); return l; }
public ReadWriteCsv.CsvRow ToCsv() { ReadWriteCsv.CsvRow l = new ReadWriteCsv.CsvRow(); l.Add(this.KycrspFundno.ToString()); l.Add(this.FrontLoad == null ? "" : this.FrontLoad); if (this.Fflbegdt == null || this.Fflbegdt.Year < 1900) l.Add(""); else l.Add(this.Fflbegdt.ToString("dd/MM/yyyy")); if (this.Fflenddt == null || this.Fflenddt.Year < 1900) l.Add(""); else l.Add(this.Fflenddt.ToString("dd/MM/yyyy")); l.Add(this.RearLoad == null ? "" : this.RearLoad); if (this.Frlbegdt == null || this.Frlbegdt.Year < 1900) l.Add(""); else l.Add(this.Frlbegdt.ToString("dd/MM/yyyy")); if (this.Frlenddt == null || this.Frlenddt.Year < 1900) l.Add(""); else l.Add(this.Frlenddt.ToString("dd/MM/yyyy")); l.Add(this.FexpRatio == null ? "" : this.FexpRatio); l.Add(this.FmgmtFee == null ? "" : this.FmgmtFee); l.Add(this.FturnRatio == null ? "" : this.FturnRatio); if (this.Ffebegdt == null || this.Ffebegdt.Year < 1900) l.Add(""); else l.Add(this.Ffebegdt.ToString("dd/MM/yyyy")); if (this.Ffeenddt == null || this.Ffeenddt.Year < 1900) l.Add(""); else l.Add(this.Ffeenddt.ToString("dd/MM/yyyy")); if (this.MCALDT == null || this.MCALDT.Year < 1900) l.Add(""); else l.Add(this.MCALDT.ToString("dd/MM/yyyy")); //l.Add(this.FMRET == null ? "" : this.FMRET); //l.Add(this.RmRfd == null ? "" : this.RmRfd); //l.Add(this.SMB == null ? "" : this.SMB); //l.Add(this.HML == null ? "" : this.HML); //l.Add(this.Mom == null ? "" : this.Mom); //l.Add(this.Rfd == null ? "" : this.Rfd); //l.Add(this._b_RmRfd == null ? "" : this._b_RmRfd); //l.Add(this._b_SMB == null ? "" : this._b_SMB); //l.Add(this._b_HML == null ? "" : this._b_HML); //l.Add(this._b_Mom == null ? "" : this._b_Mom); //l.Add(this._b_cons == null ? "" : this._b_cons); //l.Add(this._p == null ? "" : this._p); l.Add(this.TotalLoads.ToString()); return l; }
public ReadWriteCsv.CsvRow ToCsv() { ReadWriteCsv.CsvRow l = new ReadWriteCsv.CsvRow(); l.Add(this.KycrspFundno.ToString()); l.Add(this.FrontLoad == null ? "" : this.FrontLoad); if (this.Fflbegdt == null || this.Fflbegdt.Year < 1900) l.Add(""); else l.Add(this.Fflbegdt.ToString("dd/MM/yyyy")); if (this.Fflenddt == null || this.Fflenddt.Year < 1900) l.Add(""); else l.Add(this.Fflenddt.ToString("dd/MM/yyyy")); l.Add(this.RearLoad == null ? "" : this.RearLoad); if (this.Frlbegdt == null || this.Frlbegdt.Year < 1900) l.Add(""); else l.Add(this.Frlbegdt.ToString("dd/MM/yyyy")); if (this.Frlenddt == null || this.Frlenddt.Year < 1900) l.Add(""); else l.Add(this.Frlenddt.ToString("dd/MM/yyyy")); l.Add(this.FexpRatio == null ? "" : this.FexpRatio); l.Add(this.FmgmtFee == null ? "" : this.FmgmtFee); l.Add(this.FturnRatio == null ? "" : this.FturnRatio); if (this.Ffebegdt == null || this.Ffebegdt.Year < 1900) l.Add(""); else l.Add(this.Ffebegdt.ToString("dd/MM/yyyy")); if (this.Ffeenddt == null || this.Ffeenddt.Year < 1900) l.Add(""); else l.Add(this.Ffeenddt.ToString("dd/MM/yyyy")); return l; }
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); } }
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { //Boolean meshCreated = false; SaveFileDialog saveFileDialog = new SaveFileDialog(); if (saveFileDialog.ShowDialog() == DialogResult.OK) { String fileName = saveFileDialog.FileName; if (!fileName.Contains(".csv")) { fileName = fileName + ".csv"; } try { using (ReadWriteCsv.CsvFileWriter writer = new ReadWriteCsv.CsvFileWriter(fileName)) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); row.Add("50"); row.Add("50"); row.Add("20"); row.Add("20"); //Check if an object has been created //If it hasn't save default values if (mesh != null) { Logger.Debug("Slider: " + mesh.slider.ToString()); row.Add(mesh.slider.ToString()); row.Add(mesh.multi.ToString()); row.Add(mesh.shape1); if (!mesh.shape2.Equals("(null)")) { row.Add(mesh.shape2); } } else { row.Add("10"); row.Add("1"); row.Add("Square"); } writer.WriteRow(row); } } catch (IOException) { MessageBox.Show("The file you are trying to write to is open\nPlease close the file and try agian", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public void GenerateMesh(String shape) { // Write sample data to CSV file try { using (ReadWriteCsv.CsvFileWriter writer = new ReadWriteCsv.CsvFileWriter("Mesh.csv")) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); row.Add("50"); row.Add("50"); row.Add("20"); row.Add("20"); //This number is for fineness row.Add(tb_Thickness.Value.ToString()); slider = tb_Thickness.Value; if (chk_MultiMesh.Checked) { row.Add("2"); multi = 2; } else { row.Add("1"); multi = 1; } //Need to store if it is a non uniform mesh and what they are row.Add(shape); shape1 = CheckShape(1); if (chk_MultiMesh.Checked) { row.Add(CheckShape(2)); shape2 = CheckShape(2); } else { shape2 = "(null)"; } //for (int j = 0; j < 5; j++) // row.Add(String.Format("Column{0}", j)); writer.WriteRow(row); } } catch (IOException) { MessageBox.Show("The file you are trying to write to is open\nPlease close the file and try agian", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (_form1 != null) { _form1.DrawMesh(); } }
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); }
public List<Point> oneHull(List<Point> coordinates, List<Point> Lupper, List<Point> Llower, int max_Width, int max_Height, int offset, int randomness = 25) { //ConvexHull CH = new ConvexHull(); List<Point> ConvexHull = new List<Point>(); // Creates new random points //if (randomness == 30) //{ // coordinates = squarePoints(coordinates); //} //else //{ coordinates = createPoints(coordinates, max_Width, max_Height, offset, randomness); //} // Calls right turn method to find upper hull ConvexHull.AddRange(rightTurn(coordinates, Lupper)); // Calls left turn method to find lower hull ConvexHull.AddRange(leftTurn(coordinates, Llower)); try { using (ReadWriteCsv.CsvFileWriter writer = new ReadWriteCsv.CsvFileWriter("Blob.csv")) { ReadWriteCsv.CsvRow row = new ReadWriteCsv.CsvRow(); for (int i = 0; i < ConvexHull.Count; i++) { row.Clear(); row.Add(ConvexHull.ElementAt(i).X.ToString()); row.Add(ConvexHull.ElementAt(i).Y.ToString()); writer.WriteRow(row); } } } catch (IOException) { MessageBox.Show("The file you are trying to write to is open\nPlease close the file and try agian", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } return ConvexHull; }