Monday, April 1, 2013

Method to write test result to Excel c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace BusinessCreation
{
    class WritingToExcelSheet
    {
        static void Main(string[] args)
        {
            string FileName = @"C:\Users\chery\Desktop\Openings_AU.xlsx";
            string SheetName = "Sheet2";
            WriteExcelValues(FileName,SheetName,"Warning");
            WriteExcelValues(FileName,SheetName,"Fail","Desc1",true);
            WriteExcelValues(FileName,SheetName,"Pass", "Desc2", true);
            WriteExcelValues(FileName,SheetName,"Warning", "Desc3", true);
            WriteExcelValues(FileName,SheetName,"###", "Desc4", true);
            WriteExcelValues(FileName,SheetName,"Warning", "Desc5", true);
            //WriteExcelValues("Fail2", true);
            //WriteExcelValues("Fail3", true);
            //WriteExcelValues("Fail4", true);
            //Console.ReadLine();
        }

        public static void WriteExcelValues(string FileName, string SheetName, string ResultStatus, string ResultVal = "Desc", bool InsertToExstCloumn = false)
        {
            Excel.Application ExcelApp;
            Excel.Workbook WorkBook;
            Excel.Worksheet WorkSheet;
            ExcelApp = new Excel.Application();
            ExcelApp.Visible= true;
            WorkBook = (Excel.Workbook)ExcelApp.Workbooks.Open(FileName);
            WorkSheet = WorkBook.Worksheets[SheetName];
            WorkSheet.UsedRange.Columns.AutoFit();
            //Finding the last used row and column
            Excel.Range last = WorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            Excel.Range range = WorkSheet.get_Range("A1", last);
            int lastUsedRow = last.Row;
            int lastUsedColumn = last.Column;
            if (InsertToExstCloumn == true)
            {
                if (lastUsedColumn == 1 && lastUsedRow==1)
                {
                    WorkSheet.Cells[1, lastUsedColumn].Value = "Result " + 0;
                    WorkSheet.Cells[1, lastUsedColumn].Font.Bold = true;
                    WorkSheet.Cells[1, lastUsedColumn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                    WorkSheet.Cells[1, lastUsedColumn].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
                }
                int count = 1;
                for (int i = 1; i <= WorkSheet.UsedRange.Rows.Count; i++)
                {
                    if (WorkSheet.Cells[i, lastUsedColumn].Value != null)
                    {
                        count = count + 1;
                        Console.WriteLine(count);
                    }
                 }
                Console.WriteLine(count);
                if (ResultStatus == "Pass")
                {
                    WorkSheet.Cells[count, lastUsedColumn].Value = "Pass: "+ResultVal;
                    WorkSheet.Cells[count, lastUsedColumn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen);
                 
                }
                else if (ResultStatus == "Fail")
                {
                    WorkSheet.Cells[count, lastUsedColumn].Value = "Fail: "+ResultVal;
                    WorkSheet.Cells[count, lastUsedColumn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                    //WorkSheet.Cells[count, lastUsedColumn].Color = "Red";
                }
                else if (ResultStatus == "Warning")
                {
                    WorkSheet.Cells[count, lastUsedColumn].Value = "Warning: " + ResultVal;
                    WorkSheet.Cells[count, lastUsedColumn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                    //WorkSheet.Cells[count, lastUsedColumn].Color = "Red";
                }
                else if (ResultStatus == "###")
                {
                    WorkSheet.Cells[count, lastUsedColumn].Value = " " + " ";
                    //WorkSheet.Cells[count, lastUsedColumn].Color = "Red";
                }
             
            }
            else
            {
             
                WorkSheet.Cells[1, lastUsedColumn + 1].Value = "Result " + (lastUsedColumn);
                WorkSheet.Cells[1, lastUsedColumn + 1].Font.Bold=true;
                WorkSheet.Cells[1, lastUsedColumn + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                WorkSheet.Cells[1, lastUsedColumn + 1].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
                if (ResultStatus == "Pass")
                {
                    WorkSheet.Cells[2, lastUsedColumn + 1].Value = "Pass: " + ResultVal;
                    WorkSheet.Cells[2, lastUsedColumn + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen);
                }
                else if (ResultStatus == "Fail")
                {
                    WorkSheet.Cells[2, lastUsedColumn + 1].Value = "Fail: " + ResultVal;
                    WorkSheet.Cells[2, lastUsedColumn + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                }
                else if (ResultStatus == "Warning")
                {
                    WorkSheet.Cells[2, lastUsedColumn + 1].Value = "Warning: " + ResultVal;
                    WorkSheet.Cells[2, lastUsedColumn + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
                }
                else if (ResultStatus == "###")
                {
                    WorkSheet.Cells[2, lastUsedColumn + 1].Value = " " + " ";
                    //WorkSheet.Cells[count, lastUsedColumn].Color = "Red";
                }
             
            }
            WorkBook.Save();
            WorkBook.Close();
            ExcelApp.Quit();
        }
    }
}

Method to read data from Excel Sheet in C#.net

In-order to work on excel operations from c# need to add a reference Microsoft.Office.Interop.Excel
and should use the name space reference as using Excel = Microsoft.Office.Interop.Excel

Find below the c# method, ReadExcelValues to read data from an Excel sheet specifying the following inputs,
Inputs
"FileName - name of the file", 
"SheetName - name of the file",
"ColumnNumber - array[] input with column numbers"
"ColumnNumber - array[] input with column numbers,
 "Header - specify if column header value is required in the result"
Return Value
Method returns a two dimensional array with values from excel


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace BusinessCreation
{
    class ReadingFromExcel
    {
        static string[,] ExcelValList;
        static int[] ColumnNames = new int[2];
        static void Main(string[] args)
        {
            string FileName =@"C:\Users\chery\Desktop\Openings_AU.xlsx";
            string SheetName= "Sheet1";
            ColumnNames[0]= 1;
            ColumnNames[1]= 2;
            ExcelValList = ReadExcelValues(FileName, SheetName, ColumnNames,false);
            for (int i = 0; i < ExcelValList.GetLength(0); i++)
            {
                for (int j = 0; j < ExcelValList.GetLength(1); j++)
                {
                    if (ExcelValList[i, j] != null)
                    {
                        Console.WriteLine(ExcelValList[i, j]);
                    }
                    //Console.WriteLine("i :" + (i) + " j: " + (j));
                }
            }
            Console.ReadLine();
        }

        /*Input parameters for ReadExcelValues "FileName - name of the file", "SheetName - name of the file"
        Input parameters for ReadExcelValues "ColumnNumber - array[] input with column numbers"
        Input parameters for  "ColumnNumber - array[] input with column numbers, "Header - specify if column header value is required in the result"
        Return parameter multidimensional string[,] array named - ExcelValueListTemp
         */
        public static string[,] ReadExcelValues(string FileName, string SheetName, int[] ColumnNumber,bool Header)
        {
            Excel.Application ExcelApp;
            Excel.Workbook WorkBook;
            Excel.Worksheet WorkSheet;

            ExcelApp = new Excel.Application();
            ExcelApp.Visible = true;
            WorkBook = ExcelApp.Workbooks.Open(FileName);
            WorkSheet = WorkBook.Worksheets[SheetName];
            string[,] ExcelValueListTemp = new string[WorkSheet.UsedRange.Rows.Count, ColumnNumber.Length];
            for (int j = 1; j <=WorkSheet.UsedRange.Columns.Count; j++)
            {
                for (int i = 1; i <= WorkSheet.UsedRange.Rows.Count; i++)
                {
                    for (int z = 0; z < ColumnNumber.Length; z++)
                    {
                        //Consider only the columns specified in the method input "ColumnNumber - array[] input with column numbers"
                        if (j == ColumnNames[z])
                        {
                            //Flow if method input for Header is true
                            if (Header == true)
                            {
                                string str1 = WorkSheet.Cells[i, ColumnNumber[z]].Value;
                                //Condition to ignore all the null values
                                if (str1 != null)
                                {
                                    //ExcelValueListTemp.Add(ColumnNames[z].ToString() + ";" + str1);
                                    ExcelValueListTemp[i, j] = str1;
                                }
                            }
                            //Flow if method input Header is false
                            else if (Header == false)
                            {
                                if (i > 1)
                                {
                                    string str1 = WorkSheet.Cells[i, ColumnNumber[z]].Value;
                                    //Condition to ignore all the null values
                                    if (str1 != null)
                                    {
                                        ExcelValueListTemp[i - 2, ColumnNumber[z] - 1] = str1;
                                        //Console.WriteLine(ExcelValueListTemp[i - 1, ColumnNames[z] - 1]);
                                        //Console.WriteLine("i-1 :" + (i - 2) + " ColumnNames[z]-1: " + (ColumnNames[z] - 1));
                                    }
                                }
                            }
                        }

                    }
                }

            }
            WorkBook.Save();
            ExcelApp.Quit();
            return ExcelValueListTemp;

        }

    }
}