Beispiel #1
0
        /// <summary>
        /// Uses overridden .ToString() for TestExcelData to write a list of objects to a csv
        /// </summary>
        /// <param name="data"></param>
        private void WriteToStreamWithOverride(List <TestExcelData> data)
        {
            using (var fileStream = new FileStream(txt_Destination.Text, FileMode.Append))
            {
                using (var strmWrtr = new StreamWriter(fileStream))
                {
                    strmWrtr.WriteLine(string.Join(",", TestExcelData.GetPropertyDisplayNames())); //-- Headers

                    foreach (var item in data)
                    {
                        strmWrtr.WriteLine(item);
                    }
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Uses CsvHelper to write a list of objects to a csv
 /// </summary>
 /// <param name="data"></param>
 private void UseCsvHelper(List <TestExcelData> data)
 {
     using (var writer = new StreamWriter(txt_Destination.Text))
         using (var csvWrtr = new CsvWriter(writer, CultureInfo.InvariantCulture))
         {
             //-- Write headers for csv file
             foreach (string header in TestExcelData.GetPropertyDisplayNames())
             {
                 csvWrtr.WriteField(header);
             }
             csvWrtr.NextRecord();
             csvWrtr.Configuration.HasHeaderRecord = false;
             csvWrtr.WriteRecords(data);
         }
 }
        public static string[] GetPropertyDisplayNames()
        {
            var           header       = new TestExcelData();
            List <string> displayNames = new List <string>();

            displayNames.Add(typeof(TestExcelData).GetProperty("fileName").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);
            displayNames.Add(typeof(TestExcelData).GetProperty("dateCreated").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);
            displayNames.Add(typeof(TestExcelData).GetProperty("dateLastSaved").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);
            displayNames.Add(typeof(TestExcelData).GetProperty("column1").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);
            displayNames.Add(typeof(TestExcelData).GetProperty("column2").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);
            displayNames.Add(typeof(TestExcelData).GetProperty("column3").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);
            displayNames.Add(typeof(TestExcelData).GetProperty("column4").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);
            displayNames.Add(typeof(TestExcelData).GetProperty("column5").GetCustomAttributes(typeof(DisplayNameAttribute)).Cast <DisplayNameAttribute>().Single().DisplayName);

            return(displayNames.ToArray());
        }