/// <summary>
/// Converts data table to csv file
/// </summary>
/// <param name="dt">The data table to convert</param>
/// <param name="fileName">The name of the csv file</param>
/// <param name="enclosure">The enclosure string to be used</param>
/// <param name="log">The log instance to be used</param>
public static void convertDataTableToCSVFile(
DataTable dt,
string fileName,
string enclosure,
Log log)
{
log.log(String.Format("Creating CSV file to {0}", fileName));
TextWriter textWriter =
new StreamWriter(
fileName);
// Print headers to the file
foreach (DataColumn dc in dt.Columns)
textWriter.Write(enclosure + dc.Caption + enclosure + ";");
textWriter.WriteLine();
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
textWriter.Write(
enclosure +
dr[dc.Caption].ToString().Replace("\"", "\\\"").Replace(Environment.NewLine, "") +
enclosure + ";");
textWriter.WriteLine();
textWriter.Flush();
}
textWriter.Close();
log.log(String.Format("Created CSV file to {0}", fileName));
}
/// <summary>
/// Converts dd-MM-yyyy HH:mm type of string expressions to date
/// </summary>
/// <param name="s">The string to parse</param>
/// <returns>The result date</returns>
public static DateTime? parseDateWithTime(String s)
{
String pattern = @"(?<Day>\d{1,2})-(?<Month>\d{1,2})-(?<Year>\d{4}) (?<Hour>\d{2}):(?<Minute>\d{2})";
Match DateMatch = Regex.Match(s, pattern);
if (DateMatch.Success)
return new DateTime(Int32.Parse(DateMatch.Groups["Year"].Value),
Int32.Parse(DateMatch.Groups["Month"].Value),
Int32.Parse(DateMatch.Groups["Day"].Value),
Int32.Parse(DateMatch.Groups["Hour"].Value),
Int32.Parse(DateMatch.Groups["Minute"].Value),
0);
return null;
}