need help passing list to this csv export class
-
27-10-2019 - |
Question
I found this CSV export class online and would like to pass my own list from another class my list is already prepared and contains 3 columns
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using System.IO;
using System.Reflection;
public class CsvExport<T> where T : class
{
public List<T> Objects;
public CsvExport(List<T> objects)
{
Objects = objects;
}
public string Export()
{
return Export(true);
}
public string Export(bool includeHeaderLine)
{
StringBuilder sb = new StringBuilder();
//Get properties using reflection.
IList<PropertyInfo> propertyInfos = typeof(T).GetProperties();
if (includeHeaderLine)
{
//add header line.
foreach (PropertyInfo propertyInfo in propertyInfos)
{
sb.Append(propertyInfo.Name).Append(",");
}
sb.Remove(sb.Length - 1, 1).AppendLine();
}
//add value for each property.
foreach (T obj in Objects)
{
foreach (PropertyInfo propertyInfo in propertyInfos)
{
sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj, null))).Append(",");
}
sb.Remove(sb.Length - 1, 1).AppendLine();
}
return sb.ToString();
}
//export to a file.
public void ExportToFile(string path)
{
File.WriteAllText(path, Export());
}
//export as binary data.
public byte[] ExportToBytes()
{
return Encoding.UTF8.GetBytes(Export());
}
//get the csv value for field.
private string MakeValueCsvFriendly(object value)
{
if (value == null) return "";
if (value is Nullable && ((INullable)value).IsNull) return "";
if (value is DateTime)
{
if (((DateTime)value).TimeOfDay.TotalSeconds == 0)
return ((DateTime)value).ToString("yyyy-MM-dd");
return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
}
string output = value.ToString();
if (output.Contains(",") || output.Contains("\""))
output = '"' + output.Replace("\"", "\"\"") + '"';
return output;
}
}
Solution
If you want help using the class then I would say you need to do something like this.
Create a class for your data..
public class MyData
{
public string Column1Data {get;set;}
public string Column2Data {get;set;}
public string Column3Data {get;set;}
}
the pass a List of type MyData into your CsvExport class like this...
List<MyData> list = new List<MyData>();
//populate the list here
CsvExport<MyData> export = new CsvExport<MyData>();
export.ExportToFile(@"C:\MyExportFile.txt");
you should now have an output file like...
Column1Data,Column2Data,Column3Data
r1c1,r1c2,r1c3
r2c1,r2c2,r2c3
r3c1,r1c2,r3c3
To use a different input class just switch MyData references to what ever you class is called
OTHER TIPS
Here is sample of using Exporter with sample class
class Sample
{
public string Field1 {get;set;}
public int Field2 {get;set;}
}
List<Sample> source = new List<Sample>()
// fill list
CsvExport<Sample> export = new CsvExport<Sample>(source);
export.ExportToFile("yourFile.csv");
So basically create CsvExport<YourClass>
and path list of existing objects to it
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow