public static void ExportToCsvFile(List <Nation> nations, out string fileContent)
        {
            StringBuilder stringBuilder = new StringBuilder();

            ExtendedStringWriter sw = new ExtendedStringWriter(stringBuilder, Encoding.UTF8);

            // write the header
            sw.WriteLine("Name,Capital,Latitude,Longitude,ImageUri,GeographyXml");

            // write the records
            CsvWriter csv = new CsvWriter(sw);

            foreach (Nation nation in nations)
            {
                csv.WriteField(nation.Name);
                csv.WriteField(nation.Capital);
                csv.WriteField(nation.Latitude);
                csv.WriteField(nation.Longitude);
                csv.WriteField(nation.ImageUri);
                csv.WriteField(nation.GeographyXml);
                csv.NextRecord();
            }

            // tidy up
            sw.Close();

            fileContent = sw.ToString();
        }
Пример #2
0
        public static void ExportToCsvFile(List <BookRead> books, out string fileContent)
        {
            StringBuilder stringBuilder = new StringBuilder();

            ExtendedStringWriter sw = new ExtendedStringWriter(stringBuilder, Encoding.UTF8);

            // write the header
            sw.WriteLine(
                "Date,DD/MM/YYYY,Author,Title,Pages,Note,Nationality,Original Language,Book,Comic,Audio,Image,Tags"
                );

            // write the records
            var csv = new CsvWriter(sw);

            foreach (var book in books)
            {
                csv.WriteField(book.DateString);
                csv.WriteField(book.Date.ToString("d/M/yyyy"));
                csv.WriteField(book.Author);
                csv.WriteField(book.Title);
                csv.WriteField(book.Pages > 0 ? book.Pages.ToString() : "");
                csv.WriteField(book.Note);
                csv.WriteField(book.Nationality);
                csv.WriteField(book.OriginalLanguage);
                csv.WriteField(book.Format == BookFormat.Book ? "x" : "");
                csv.WriteField(book.Format == BookFormat.Comic ? "x" : "");
                csv.WriteField(book.Format == BookFormat.Audio ? "x" : "");
                csv.WriteField(book.ImageUrl);
                csv.WriteField(book.DisplayTags);
                csv.NextRecord();
            }

            // tidy up
            sw.Close();

            fileContent = sw.ToString();
        }