using System; using System.Data; using System.IO; using System.Windows; namespace AuroraLaserScanner.Utils { internal class CSVUtils { public static void ExportToCSV(string path, DataTable table) { if (path.Substring(path.Length - 4, 4).Contains(".csv") == false) { path += ".csv"; } try { if (Directory.Exists(Path.GetDirectoryName(path)) == false) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } StreamWriter sw = new StreamWriter(path, false); string columnHeader = ""; for (int i = 0; i < table.Columns.Count; i++) { columnHeader += table.Columns[i].ColumnName + (i != table.Columns.Count - 1 ? ", " : ""); } sw.WriteLine(columnHeader); string strText; for (int i = 0; i < table.Rows.Count; i++) { strText = ""; for (int j = 0; j < table.Columns.Count; j++) { strText += table.Rows[i][j].ToString() + ","; } sw.WriteLine(strText); } sw.Close(); } catch (Exception ex) { MessageBox.Show(string.Format("fail to save csv file.\n{0}", ex.Message)); } } public static void ExportToLDDFileCSV(string path, DataTable table) { if (path.Substring(path.Length - 4, 4).Contains(".csv") == false) { path += ".csv"; } try { if (Directory.Exists(Path.GetDirectoryName(path)) == false) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } StreamWriter sw = new StreamWriter(path, false); //우선 선언해두고 다른데서 받아오는걸로 변경하자 double dLDDFileVersion = 1.0; string sLDDFileType = "Path Coordinates"; double dLDDRecordTime = 0.00001; sw.WriteLine($"File Version: {dLDDFileVersion}"); sw.WriteLine($"File Type: {sLDDFileType}"); sw.WriteLine($"Timebase (s): {dLDDRecordTime.ToString("F6")}"); sw.WriteLine(); string columnHeader = ""; for (int i = 0; i < table.Columns.Count; i++) { columnHeader += table.Columns[i].ColumnName + (i != table.Columns.Count - 1 ? ", " : ""); } sw.WriteLine(columnHeader); string strText; for (int i = 0; i < table.Rows.Count; i++) { strText = ""; for (int j = 0; j < table.Columns.Count; j++) { strText += table.Rows[i][j].ToString() + ","; } sw.WriteLine(strText); } sw.Close(); } catch (Exception ex) { MessageBox.Show(string.Format("fail to save csv file.\n{0}", ex.Message)); } } } }