/// <summary> /// Saves a list of entries to a csv file. /// </summary> /// <param name="streamWriter">Stream for recording.</param> public void SaveToCsv(StreamWriter streamWriter) { if (streamWriter == null) { throw new ArgumentNullException(nameof(streamWriter)); } var writer = new FileCabinetRecordCsvWriter(streamWriter); streamWriter.WriteLine("Id, First name, Last name, Date of Birth, Wallet, Marital status, Height"); foreach (var record in this.records) { writer.Write(record); } }
private static void Export(Options options, IEnumerable <FileCabinetRecord> records) { if (File.Exists(options.OutputFileName)) { Console.WriteLine($"File is exist - rewrite {options.OutputFileName}? [Y/n]"); string answer = Console.ReadLine(); if (answer.Equals(Configurator.GetConstantString("PositiveAnswer"), StringComparison.OrdinalIgnoreCase)) { File.Delete(options.OutputFileName); } else { return; } } if (options.OutputType.Equals(Configurator.GetConstantString("Csv"), StringComparison.OrdinalIgnoreCase)) { try { using StreamWriter streamWriter = new StreamWriter(options.OutputFileName); streamWriter.WriteLine(Configurator.GetConstantString("CsvHeader")); using FileCabinetRecordCsvWriter csvWriter = new FileCabinetRecordCsvWriter(streamWriter); foreach (FileCabinetRecord record in records) { csvWriter.Write(record); } Console.WriteLine($"{records.Count()} records are exported to file {options.OutputFileName}"); } catch (DirectoryNotFoundException) { Console.WriteLine($"Can't find directory {options.OutputFileName}."); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); } } else if (options.OutputType.Equals(Configurator.GetConstantString("Xml"), StringComparison.OrdinalIgnoreCase)) { FileCabinetRecordXml[] xmlRecords = new FileCabinetRecordXml[records.Count()]; int counter = 0; foreach (FileCabinetRecord record in records) { xmlRecords[counter] = new FileCabinetRecordXml(record); counter++; } XmlWriterSettings settings = new XmlWriterSettings { Indent = true, }; try { using (var fileWriter = XmlWriter.Create(options.OutputFileName, settings)) { XmlWriterToGenerator xmlWriter = new XmlWriterToGenerator(fileWriter, xmlRecords); xmlWriter.Write(); } Console.WriteLine($"{records.Count()} records are exported to file {options.OutputFileName}"); } catch (DirectoryNotFoundException) { Console.WriteLine($"Can't find directory {options.OutputFileName}."); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); } } else { Console.WriteLine($"Wrong format type {options.OutputType}."); Console.WriteLine(Configurator.GetConstantString("ClosingProgram")); } }