Sunday 16 August 2015

How do I convert a text file to an Excel file with validation using C#?


Text file Format

C# Code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

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

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            Excel.Range chartRange;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            string line;
            using (StreamReader reader = new StreamReader(@"yourFilePath.txt"))
            {
                int lineNo = 4;
                ArrayList list = new ArrayList();
                int[] arr4 = new int[100];
                while ((line = reader.ReadLine()) != null)
                {
                    
                    int colomnNo = 2;
                    string[] words = line.Split('|');
                    int startno = 0;
                    if (line.StartsWith("Sriram validation"))
                    {
                        string output = line.Replace("Sriram validation | ", "");
                        string[] numbers = output.Split('|');
                       
                        foreach (string nos in numbers)
                        {   
                            arr4[startno] = Convert.ToInt16(nos);
                            startno++;
                        }
                    }
                    else
                        foreach (string word in words)
                        {
                            string wordcount = word.Trim();
                            xlWorkSheet.Cells[lineNo, colomnNo] = wordcount;
                            if (arr4[startno] == wordcount.Length)
                                 xlWorkSheet.Cells[lineNo, colomnNo].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
                            else if (arr4[startno] < wordcount.Length)
                                xlWorkSheet.Cells[lineNo, colomnNo].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                            else if (arr4[startno] > wordcount.Length)
                                xlWorkSheet.Cells[lineNo, colomnNo].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);
                            colomnNo++;
                            startno++;
                        }
                    lineNo++;
                }
            }
            xlWorkSheet.get_Range("b2", "e3").Merge(false);

            chartRange = xlWorkSheet.get_Range("b2", "e3");
            chartRange.FormulaR1C1 = " Validation";
            chartRange.HorizontalAlignment = 3;
            chartRange.VerticalAlignment = 3;
            chartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
            chartRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
            chartRange.Font.Size = 20;

            xlWorkBook.SaveAs("d:\\csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlApp);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkSheet);
        }

        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                //MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}

Output :