Click here to Skip to main content
15,887,379 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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
{
    /// <summary>
    /// The program.
    /// </summary>
    internal class Program
    {
        #region Methods

        /// <summary>
        /// The export data from a text file to an excel file.
        /// </summary>
        /// <param name="excellWorkSheet" />
        /// The excell work sheet. 
        /// 
        /// <param name="fileName" />
        /// The file name. 
        /// 
        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++;
            }
        }

        /// <summary>
        /// The close quit and release.
        /// </summary>
        /// <param name="excellApp" />
        /// The excell app. 
        /// 
        /// <param name="excellWorkSheet" />
        /// The excell work sheet. 
        /// 
        /// <param name="excellWorkBook" />
        /// The excell work book. 
        /// 
        /// <param name="misValue" />
        /// The mis value. 
        /// 
        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);
        }

        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args" />
        /// The args. 
        /// 
        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];

            /* Data(textFile to excellWorkSheet); */
            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();
        }

        /// <summary>
        /// The release object.
        /// </summary>
        /// <param name="obj" />
        /// The obj. 
        /// 
        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();
            }
        }

        /// <summary>
        /// The safe.
        /// </summary>
        /// <param name="misValue" />
        /// The mis value. 
        /// 
        /// <param name="excellWorkBook" />
        /// The excell work book. 
        /// 
        /// <param name="excellfilename" />The name of the excell file 
        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
    }
}
Posted
Updated 25-Aug-17 14:40pm
v3
Comments
BillWoodruff 25-Aug-17 13:16pm    
What are you doing now to debug your program and change its behavior ? Describe any specific errors.
[no name] 26-Aug-17 20:21pm    
Thanks for your concern and assistance. The code I am using (listed above) seems to pull data from a flat file (word document) and puts it in an excel sheet but it just throws all the text in with no specific order. I am trying to get only specific parts and put them in specific columns and rows in an excel sheet. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900