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);
}
}