Monday, April 1, 2013

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;

        }

    }
}

No comments:

Post a Comment