Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
itbs-roa-convert-file/Program.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
162 lines (146 sloc)
6.89 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Data; | |
using System.IO; | |
using ExcelDataReader; | |
using System.Net.Mail; | |
namespace itbs_roa_convert_file | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
//Convert file and note success if no errors encountered. | |
ConvertFile(); | |
} | |
catch (Exception ex) | |
{ | |
//Write error to log file. | |
WriteToLog("ERROR: Error encountered in ITBS ROA Convert Files Main - " + ex.Message, true); | |
} | |
} | |
//------------------------------------------------------------------------------------------ | |
//ConvertFile - Converts file into delimited file. | |
//------------------------------------------------------------------------------------------ | |
static void ConvertFile() | |
{ | |
try | |
{ | |
//Load in delimiter. | |
string delimiter = Properties.Settings.Default.Delimiter; | |
//Open new file for output. | |
string outFilename = Properties.Settings.Default.NewFile; | |
using (StreamWriter outFile = new StreamWriter(outFilename, false, Encoding.ASCII)) | |
{ | |
// Open Cognos report file for input. | |
using (var stream = File.Open(Properties.Settings.Default.CognosReport, FileMode.Open, FileAccess.Read)) | |
{ | |
// Open excel reader stream. | |
using (var reader = ExcelReaderFactory.CreateReader(stream)) | |
{ | |
// Read in file a line at a time and save to output file. | |
do | |
{ | |
int counter = 0; | |
while (reader.Read()) | |
{ | |
counter++; | |
//Handle first header line of file. | |
if(counter == 1) | |
{ | |
outFile.WriteLine(reader.GetString(0)); | |
} | |
//Handle second header line of file. | |
else if (counter == 2) | |
{ | |
outFile.Write(reader.GetString(0) + delimiter); | |
outFile.Write(reader.GetString(1) + delimiter); | |
outFile.Write(reader.GetString(2) + delimiter); | |
outFile.Write(reader.GetString(3) + delimiter); | |
outFile.Write(reader.GetString(4) + delimiter); | |
outFile.WriteLine(reader.GetString(5)); | |
} | |
//Handle standard data rows. | |
else | |
{ | |
outFile.Write(reader.GetDouble(0).ToString() + delimiter); | |
outFile.Write(reader.GetString(1) + delimiter); | |
outFile.Write(reader.GetString(2) + delimiter); | |
outFile.Write(reader.GetDouble(3).ToString() + delimiter); | |
outFile.Write(reader.GetDouble(4).ToString() + delimiter); | |
outFile.WriteLine(reader.GetString(5)); | |
} | |
} | |
} while (reader.NextResult()); | |
} | |
} | |
//Write success to log file. | |
WriteToLog(Properties.Settings.Default.NewFile + " successfully created.", false); | |
} | |
} | |
catch (Exception ex) | |
{ | |
//Write error to log file. | |
WriteToLog("ERROR: Error encountered in ITBS ROA Convert Files ConvertFile Function - " + ex.Message, true); | |
} | |
} | |
//------------------------------------------------------------------------------------------ | |
//WriteToLog - Writes messages to log file if available and to console if not available. | |
//------------------------------------------------------------------------------------------ | |
static void WriteToLog(string message, bool isError = false) | |
{ | |
try | |
{ | |
//Load name of log file where messages are to be saved. | |
string logFilename = string.Format("{0}\\{1:yyyyMM}.log", Properties.Settings.Default.LogFolder, DateTime.Now); | |
//Update message to include date and time stamp in front. | |
message = string.Format("{0:MM/dd/yyyy hh:mm tt} - {1}", DateTime.Now, message); | |
//If we don't have a log file display message to console. | |
if (logFilename == "") | |
{ | |
Console.WriteLine(message); | |
Console.ReadKey(); | |
} | |
else | |
{ | |
//Write message to log file. | |
using (StreamWriter lFile = new StreamWriter(logFilename, true)) | |
{ | |
lFile.WriteLine(message); | |
} | |
} | |
//If we have an error send an alert. | |
if (isError == true) SendEmailAlert(message); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("ERROR: Error encountered in WriteToLog - " + ex.Message); | |
} | |
} | |
//------------------------------------------------------------------------------------------ | |
//SendEmailAlert - Sends email alert when errors encountered. | |
//------------------------------------------------------------------------------------------ | |
static void SendEmailAlert(string message) | |
{ | |
try | |
{ | |
//Prepare email message. | |
MailMessage email = new MailMessage(Properties.Settings.Default.AlertsFromEmailAddress, Properties.Settings.Default.AlertsToEmailAddress); | |
email.Subject = Properties.Settings.Default.AlertsSubject; | |
email.Body = message; | |
//Send email. | |
SmtpClient client = new SmtpClient(Properties.Settings.Default.SmtpHost, Properties.Settings.Default.SmtpPort); | |
client.Send(email); | |
} | |
catch (Exception ex) | |
{ | |
WriteToLog("ERROR: Error encountered sending email alert (SendEmailAlert) - " + ex.Message, false); | |
} | |
} | |
} | |
} |