public static void WriteOut <T>(Csv.Type.Common.CommonCsvList <T> sourceObject, List <string> headerList, out int count) { count = 0; //Create a Header string line - with quotes & delimiter if (headerList.Count == 0) { CsvMaker.GetDefaultFields <T>(ref headerList); sourceObject.HeaderString = CsvMaker.CsvLine(headerList); } else { sourceObject.HeaderString = CsvMaker.CsvLine(headerList); }; using (StreamWriter sw = new StreamWriter(sourceObject.OutputPath, true)) { //Write header string to file sw.WriteLine(sourceObject.HeaderString); foreach (var item in sourceObject.Items) { //Create a csv line - with quotes and delimiter sw.WriteLine(CsvMaker.CsvItem <T>(item)); sourceObject.Count++; } sw.WriteLine(EndofFileString()); } }
public static void WriteOut <T>(Csv.Type.Common.CommonCsvList <T> sourceObject) { //Create a Header string line - with quotes & delimiter var header = ""; if (sourceObject.HeaderList.Count == 0) { CsvMaker.GetDefaultFields <T>(ref sourceObject.HeaderList); header = CsvMaker.CsvLine(sourceObject.HeaderList); } else { CsvMaker.CsvLine(sourceObject.HeaderList); } using (StreamWriter sw = new StreamWriter(sourceObject.OutputPath, true)) { //Write header string to file sw.WriteLine(header); foreach (var item in sourceObject.Items) { sw.WriteLine(CsvMaker.CsvItem <T>(item)); } sw.WriteLine(EndofFileString()); } }
public static void WriteCSVFile(string sourceFileName, string outputPath, string payrollNo) { /* Validate */ var dir = Path.GetDirectoryName(outputPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var fileName = CommonService.FileName("contacts", payrollNo); outputPath = Path.Combine(outputPath, fileName); if (File.Exists(outputPath)) { File.Delete(outputPath); } /* Read Source */ var engine = new CsvParser(sourceFileName, true); var contactList = new Csv.Type.Common.CommonCsvList <Csv.Type.Contact.Contact>(); while (!engine.EndOfStream) { engine.ReadLine(); if (engine.CsvHeader.Count == engine.CsvLine.Count) { contactList.Items.Add(ContactData.GetLine(engine)); } } contactList.OutputPath = outputPath; /* Write CSV */ CommonService.WriteOut <Csv.Type.Contact.Contact>(contactList); int count = contactList.Count; }
public static void ReadPayrollFile(string fileName) { var engine = new CsvParser(fileName, true); var payrollList = new Csv.Type.Common.CommonCsvList <Csv.Type.Payroll.Payroll>(); while (!engine.EndOfStream) { engine.ReadLine(); if (engine.CsvHeader.Count == engine.CsvLine.Count) { payrollList.Items.Add(PayrollData.GetLine(engine)); Console.WriteLine($"AccountOfficeReference : {engine.CsvItem("AccountOfficeReference")}"); Console.WriteLine($"Address1 : {engine.CsvItem("Address1")}"); Console.WriteLine($"CompanyName : {engine.CsvItem("CompanyName")}"); Console.WriteLine($"FourWeeklyDivisor : {engine.CsvItem("FourWeeklyDivisor")}"); Console.WriteLine($"TaxDistrict : {engine.CsvItem("TaxDistrict")}"); Console.WriteLine($"NormalPayDay : {engine.CsvItem("NormalPayDay")}"); Console.WriteLine($"EmployeeNoFormat : {engine.CsvItem("EmployeeNoFormat")}"); Console.WriteLine($"OmniSlip : {engine.CsvItem("OmniSlip")}"); Console.WriteLine($"PrintPaymentDate : {engine.CsvItem("PrintPaymentDate")}"); Console.WriteLine("-----------------------------------------------"); } else if (engine.CsvLine.Count > 0 && engine.CsvLine[0].Contains("EOF")) { payrollList.EndOfFile = true; payrollList.EndOfFileTimeStamp = (engine.CsvLine.Count < 2) ? "" : engine.CsvLine[1]?.ToString(); Console.WriteLine($"End of CSV File : Create TimeStamp @ {payrollList.EndOfFileTimeStamp}"); } else { Console.WriteLine($"Error line : { engine.ErrorRow}"); } var x = new Csv.Type.Payroll.Payroll(); CsvLib.WireupCSV <Csv.Type.Payroll.Payroll>(ref x, engine.CsvHeader, engine.CsvLine); var y = x.Address1; } Console.WriteLine($"Total Parsed Count : {payrollList.Count()}"); Console.WriteLine(engine.CsvItem("Address2")); }
public static Csv.Type.Common.CommonCsvList <Csv.Type.Payroll.Payroll> ReadFromSource(string sourceFileName, string outputPath) { var engine = new CsvParser(sourceFileName, true); var payrollList = new Csv.Type.Common.CommonCsvList <Csv.Type.Payroll.Payroll>(); while (!engine.EndOfStream) { engine.ReadLine(); if (engine.CsvHeader.Count == engine.CsvLine.Count) { payrollList.Items.Add(PayrollData.GetLine(engine)); } else if (engine.CsvLine.Count > 0 && engine.CsvLine[0].Contains("EOF")) { payrollList.EndOfFile = true; } payrollList.EndOfFileTimeStamp = (engine.CsvLine.Count < 2) ? "" : engine.CsvLine[1]?.ToString(); } payrollList.OutputPath = outputPath; return(payrollList); }