public static List <RacerDetail> BuildTable(DataTable table) { List <RacerDetail> racers = new List <RacerDetail>(); try { for (int i = 0; i < 20; i++) { var racer = new RacerDetail(); DataRow row = table.Rows[i]; int slNo; var isGreaterThanZero = int.TryParse(row.ItemArray[0].ToString(), out slNo); if (!isGreaterThanZero) { continue; } racer.SlNo = row.ItemArray[0]?.ToString(); racer.Number = row.ItemArray[1]?.ToString(); racer.Name = row.ItemArray[2]?.ToString(); racer.Gap = row.ItemArray[3]?.ToString(); racer.TotalTime = row.ItemArray[4]?.ToString(); racer.Fastest = row.ItemArray[5]?.ToString(); racer.In = Convert.ToInt32(row.ItemArray[6]?.ToString()); racer.AverageSpeed = Convert.ToDecimal(row.ItemArray[7]?.ToString()); racers.Add(racer); } } catch (Exception ex) { Logger.Info("XlsReaderSErvce.BuildTable Error:" + ex.Message); } return(racers); }
private static void WriteToFile(List <RacerDetail> racer, string fileLoction, FileType fileType) { var racerCount = racer.Count; int count = 20 - racerCount; for (int i = 0; i <= count - 1; i++) { var rac = new RacerDetail(); racer.Add(rac); } StringBuilder sb = new StringBuilder(); int cnIndex = 1; foreach (var item in racer) { if (fileType == FileType.CN) { sb.Append("<C" + cnIndex + "\t" + item.Number); } else if (fileType == FileType.ST) { sb.Append("<T" + cnIndex + "\t" + item.TotalTime); } else if (fileType == FileType.Name) { sb.Append("<T" + cnIndex + "\t" + item.Name); } else if (fileType == FileType.FT) { if (cnIndex == 1) { sb.Append("<FT1" + "\t" + item.Fastest); } } sb.Append(System.Environment.NewLine); cnIndex++; } try { using (Stream file1 = File.Open(fileLoction, System.IO.FileMode.Create)) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(file1)) { file.Write(sb); } } } catch (UnauthorizedAccessException ex) { Logger.Info(ex.Message); } catch (Exception ex) { Logger.Info(ex.Message); } }