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 :


Saturday 15 August 2015

text to speech in c#



First, we need to reference the System.Speech assembly from the application. From the Project menu, choose Add Reference. The from the .NET tab, highlight System.Speech and choose OK.

Declarations

Next, we need to declare and instantiate a speech object. The class isSystem.Speech.Synthesis.Speechsynthesizer. This one class has enough properties and methods to speak a string using the default language and voice of the OS. In Microsoft Windows Vista, the default voice is Microsoft Ana. 


protected void ibtnSpeaker_Click(object sender, ImageClickEventArgs e)
        {
            Thread t = new Thread(() =>
            {
                SpeechSynthesizer audio = new SpeechSynthesizer();
                audio.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Senior); // to change VoiceGender and VoiceAge check out those links below
                audio.Volume = 100;  // (0 - 100)
                audio.Rate = -1;     // (-10 - 10)
                audio.Speak(lblQuestionStart.Text);
            });
            t.Start();
        }