I am trying to parse out certain data in a Microsoft Outlook email .pst file using C# to create the program for putting the data results into an excel spreadsheet.
Below is a snippet of what each email header and body looks like. (names have been changed for obvious reasons.)
______________________________________________________________________
Sent: Wednesday, February 22, 2017 8:51 AM
To: John Dillinger <john.dillinger@outlook.com>
Subject: RE: Night Processing Timeline - February 15, 2017
Company1:
Data files available on FTP server at 1:05 AM
Data import completion notice from Q1 received at 1:43 AM
Transaction Volume: 233,263
Company2:
Data files available on FTP server at 2:31 AM
Data import completion notice from Q1 received at 3:40 AM
Transaction Volume: 352,294
______________________________________________________________________
The data I am looking for (in the blue text above) should be broken into 5 columns displaying the results for both companies.
The columns should look like this in the excel sheet.
Date Company FTP Time Q2 Time Quantity
2/15/2017 Company1 1:05 AM 1:43 AM 15423
2/15/2017 Company2 2:31 AM 3:40 AM 782654
In the excel snippet above, “Quantity” = “Transact Volume” from the email.
The parser should get the date from the “Subject Line” not the “Sent” line and the company, quantity and times from the main body of the email while ignoring everything else.
I am new to C# programming and have never created a parser. I would appreciate any assistance on this.
Thank you.
If you need additional clarification of what my goal is; please email me at pailwriter at [e-mail address removed]
What I have tried:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace ConsoleApplication2
{
internal class Program
{
#region Methods
private static void AddDataFromFile(_Worksheet excellWorkSheet, string fileName)
{
if (excellWorkSheet == null)
{
throw new ArgumentNullException("excellWorkSheet");
}
var lines = File.ReadAllLines(fileName);
var rowCounter = 1;
foreach (var line in lines)
{
var columnCounter = 1;
var values = line.Split(' ');
foreach (var value in values)
{
excellWorkSheet.Cells[rowCounter, columnCounter] = value;
columnCounter++;
}
rowCounter++;
}
}
private static void CloseQuitAndRelease(
Application excellApp, Worksheet excellWorkSheet, Workbook excellWorkBook, object misValue)
{
excellWorkBook.Close(true, misValue, misValue);
excellApp.Quit();
ReleaseObject(excellApp);
ReleaseObject(excellWorkBook);
ReleaseObject(excellWorkSheet);
}
private static void Main(string[] args)
{
object misValue = System.Reflection.Missing.Value;
var excellApp = new Application();
var excellWorkBook = excellApp.Workbooks.Add(misValue);
var excellWorkSheet = (Worksheet)excellWorkBook.Worksheets.Item[1];
const string InputfileName = "emailFile.txt";
AddDataFromFile(excellWorkSheet, InputfileName);
Safe(misValue, excellWorkBook, "emailFile.xls");
CloseQuitAndRelease(excellApp, excellWorkSheet, excellWorkBook, misValue);
Console.WriteLine("File created !");
Console.WriteLine("File placed in C:_Users_alove_Documents");
Console.ReadLine();
}
private static void ReleaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
}
catch (Exception ex)
{
Console.WriteLine("Unable to release the Object " + ex);
}
finally
{
GC.Collect();
}
}
private static void Safe(object misValue, Workbook excellWorkBook, string excellfilename)
{
excellWorkBook.SaveAs(
excellfilename,
XlFileFormat.xlWorkbookNormal,
misValue,
misValue,
misValue,
misValue,
XlSaveAsAccessMode.xlExclusive,
misValue,
misValue,
misValue,
misValue,
misValue);
}
#endregion
}
}