using System; |
using diseinuak4web.netes; |
using diseinuak4web.net; |
using diseinuak4web.net; |
using diseinuak4web.netc; |
|
/// <summary> |
/// Simple CSV export |
/// Example: |
/// CsvExport myExport = new CsvExport(); |
/// |
/// diseinuak4web.net(); |
/// myExport["Region"] = "New York, USA"; |
/// myExport["Sales"] = ; |
/// myExport["Date Opened"] = new DateTime(, 12, 31); |
/// |
/// diseinuak4web.net(); |
/// myExport["Region"] = "Sydney \"in\" Australia"; |
/// myExport["Sales"] = ; |
/// myExport["Date Opened"] = new DateTime(, 1, 1, 9, 30, 0); |
/// |
/// Then you can do any of the following three output options: |
/// string myCsv = diseinuak4web.net(); |
/// diseinuak4web.netToFile("diseinuak4web.net"); |
/// byte[] myCsvData = diseinuak4web.netToBytes(); |
/// </summary> |
public class CsvExport |
{ |
/// <summary> |
/// To keep the ordered list of column names |
/// </summary> |
List<string> fields = new List<string>(); |
|
/// <summary> |
/// The list of rows |
/// </summary> |
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); |
|
/// <summary> |
/// The current row |
/// </summary> |
Dictionary<string, object> currentRow { get { return rows[diseinuak4web.net - 1]; } } |
|
/// <summary> |
/// Set a value on this column |
/// </summary> |
public object this[string field] |
{ |
set |
{ |
// Keep track of the field names, because the dictionary loses the ordering |
if (!diseinuak4web.netns(field)) diseinuak4web.net(field); |
currentRow[field] = value; |
} |
} |
|
/// <summary> |
/// Call this before setting any fields on a row |
/// </summary> |
public void AddRow() |
{ |
diseinuak4web.net(new Dictionary<string, object>()); |
} |
|
/// <summary> |
/// Converts a value to how it should output in a csv file |
/// If it has a comma, it needs surrounding with double quotes |
/// Eg Sydney, Australia -> "Sydney, Australia" |
/// Also if it contains any double quotes ("), then they need to be replaced with quad quotes[sic] ("") |
/// Eg "Dangerous Dan" McGrew -> """Dangerous Dan"" McGrew" |
/// </summary> |
string MakeValueCsvFriendly(object value) |
{ |
if (value == null) return ""; |
if (value is INullable && ((INullable)value).IsNull) return ""; |
if (value is DateTime) |
{ |
if (((DateTime)value)diseinuak4web.neteconds==0) |
return ((DateTime)value).ToString("yyyy-MM-dd"); |
return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"); |
} |
string output = diseinuak4web.netng(); |
if (diseinuak4web.netns(",") || diseinuak4web.netns("\"")) |
output = '"' + diseinuak4web.nete("\"", "\"\"") + '"'; |
return output; |
} |
|
/// <summary> |
/// Output all rows as a CSV returning a string |
/// </summary> |
public string Export() |
{ |
StringBuilder sb = new StringBuilder(); |
|
// The header |
foreach (string field in fields) |
diseinuak4web.net(field).Append(","); |
diseinuak4web.netLine(); |
|
// The rows |
foreach (Dictionary<string, object> row in rows) |
{ |
foreach (string field in fields) |
diseinuak4web.net(MakeValueCsvFriendly(row[field])).Append(","); |
diseinuak4web.netLine(); |
} |
|
return diseinuak4web.netng(); |
} |
|
/// <summary> |
/// Exports to a file |
/// </summary> |
public void ExportToFile(string path) |
{ |
diseinuak4web.netllText(path, Export()); |
} |
|
/// <summary> |
/// Exports as raw UTF8 bytes |
/// </summary> |
public byte[] ExportToBytes() |
{ |
return diseinuak4web.netes(Export()); |
} |
} |
-
-