I am trying to remove empty rows in excel file using npoi with C#. i am facing the problem with my code can any one help on this please.
public static void removeRow(string filepath,string SheetName)
{
try {
IWorkbook workBook = WorkbookFactory.Create(new FileStream(
Path.GetFullPath(filepath),
FileMode.Open, FileAccess.Read,
FileShare.ReadWrite));
ISheet workSheet = workBook.GetSheet(SheetName);
int lastRowNum = workSheet.LastRowNum;
for (int i = 0; i < lastRowNum; i++)
{
string value = ExcelUtils.getCellData(filepath, SheetName, i, 1);
if (value.Equals(""))
{
workSheet.ShiftRows(i + 1, lastRowNum, -1);
lastRowNum = workSheet.LastRowNum;
i--;
using (FileStream stream = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite))
{
workBook.Write(stream);
stream.Close();
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Unable to get Data from Sheet. Exception is : " + e);
}
}
my problem is when an empty row found in sheet workSheet.ShiftRows() method shifts up the empty row then lastRowNum(total rows in sheet) count decreases by 1. its working for first time, but when the next time empty row found lastRownum value is increasing but not decreasing.