0

I have an excel sheet with three column (Name, Gender, Email, Salary), I want to write a console application using NOPI library to select all male data with highest salary

I use this code to read file from xl

using System;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

private static void procExcel(string fileName, string schoolPicDir){
    try
    {
        IWorkbook workbook;
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        if (fileName.IndexOf(".xlsx") > 0) 
            workbook = new XSSFWorkbook(fs);
        else if (fileName.IndexOf(".xls") > 0) 
            workbook = new HSSFWorkbook(fs);
        //First sheet
        ISheet sheet = workbook.GetSheetAt(0);
        if (sheet != null)
        { 
            int rowCount = sheet.LastRowNum; // This may not be valid row count.
            // If first row is table head, i starts from 1
            for (int i = 1; i <= rowCount; i++)
            {
                IRow curRow = sheet.GetRow(i);
                // Works for consecutive data. Use continue otherwise 
                if (curRow == null)
                {
                    // Valid row count
                    rowCount = i - 1;
                    break;
                }
                // Get data from the 4th column (4th cell of each row)
                var cellValue = curRow.GetCell(3).StringCellValue.Trim();
                Console.WriteLine(cellValue);
            }
        }
    }
    catch(Exception e)
    {
        Console.WriteLine(e.Message);
    }
}
  • Where is your problem and what did you try to solve it? – josibu Mar 20 '21 at 20:16
  • What should I do to select all male data with highest salary? – Hala Al-khaldy Mar 20 '21 at 20:24
  • Declare a global variable, set it to the salary of the forst male you iterate over. Then, as you iterate over more people, check if it is a male and if it is, and his salary is greater than what you've stored in the global variable, then update the global variable with the current salary, otherwise `continue`. – josibu Mar 20 '21 at 20:27
  • If more than one male name has the highest salary, then you will need store those values into a “collection” type data structure. Something like a list. Example... Add the first item to the list. If the next item is “greater than” the current item, then, "empty" the list and add the new item. If the next item is “equal” to current item in the list, then simply add that item to the list without removing the current item(s). – JohnG Mar 20 '21 at 20:56

0 Answers0