Monday 17 June 2013

Reading Data From Text File In C#, XPO Format

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Microsoft.Office.Interop.Excel.Workbook mWorkBook;
            Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
            Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
            Microsoft.Office.Interop.Excel.Application oXL;
            int counter = 1; string line;
            string tempStr;
            StringBuilder sb = new StringBuilder();
            oXL =  new Microsoft.Office.Interop.Excel.Application();
            mWorkBook = oXL.Workbooks.Open(@"C:\Users\axuser\Desktop\Test.xlsx", 0, false, 5, "", "",   false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

            oXL.Visible = true;
            mWorkSheets = mWorkBook.Worksheets;
            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item ("Sheet1");
            // Read the file and display it line by line.            
            using (System.IO.StreamReader file = new   System.IO.StreamReader(@"C:\Users\axuser\Desktop\Test.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("CONFIGURATIONKEY #"))
                    {
                        // This append the text and a newline into the StringBuilder buffer      
                        //sb.AppendLine(line.ToString());
                        tempStr = line.Substring(20, line.Length - 20);
                        mWSheet1.Cells[counter, 1] = tempStr;
                        counter++;
                    }
                }
            }
            //txtResult.Text = sb.ToString();
        }
    }
}

1 comment: