Quantcast
Channel: Active questions tagged nuget-package - Stack Overflow
Viewing all articles
Browse latest Browse all 3067

Want to read data from different Excel worksheets in the same Excel file using selenium c#

$
0
0

Have used ExcelDataReader NuGET Package.

This is the class setting it up -

    private static DataTable ExcelToDataTable(string fileName)    {        //open file and returns as Stream        FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);        //Createopenxmlreader via ExcelReaderFactory        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx                                                                                       //Set the First Row as Column Name        excelReader.IsFirstRowAsColumnNames = true;        //Return as DataSet        DataSet result = excelReader.AsDataSet();        //Get all the Tables        DataTableCollection table = result.Tables;        //Store it in DataTable        DataTable resultTable = table["Sheet1"];        //return        return resultTable;    }   static List<Datacollection> dataCol = new List<Datacollection>();public static void PopulateInCollection(string fileName)    {        DataTable table = ExcelToDataTable(fileName);        //Iterate through the rows and columns of the Table        for (int row = 1; row <= table.Rows.Count; row++)        {            for (int col = 0; col < table.Columns.Count; col++)            {                Datacollection dtTable = new Datacollection()                {                    rowNumber = row,                    colName = table.Columns[col].ColumnName,                    colValue = table.Rows[row - 1][col].ToString()                };                //Add all the details for each row                dataCol.Add(dtTable);            }        }    }    public static string ReadData(int rowNumber, string columnName)    {        try        {            //Retriving Data using LINQ to reduce much of iterations            string data = (from colData in dataCol                           where colData.colName == columnName && colData.rowNumber == rowNumber                           select colData.colValue).SingleOrDefault();            //var datas = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;            return data.ToString();        }        catch (Exception)        {            return null;        }    }}public class Datacollection{    public int rowNumber { get; set; }    public string colName { get; set; }    public string colValue { get; set; }}

}


It called in the code like -

   //setting the excel file locationExcelLib.PopulateInCollection(@"C:\Users\User1\Documents\data.xlsx");//reading data from row 1 & column named middlenamexrmApp.Entity.SetValue("middlename", ExcelLib.ReadData(1, "middlename"));

I want to be able to use this code in different method and for each method I need different data.So is their anyway I can add a worksheet number parameter to the code so I can specify which worksheet to look into when reading the row number and column number.

Thanks!


Viewing all articles
Browse latest Browse all 3067

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>