So im using a for loop to loop through all rows in an excel spreadsheet and i can display the current row number very easily by just using the "i" definition, however it prints on multiple lines since each iteraton displays with a Console.WriteLine() command.
What i would like is for it to only show it once, and have it display an updated iteration on one single line. Here is my current code:
void DeleteCells(string filePath)
{
int currRowNumber = 0;
// create excel-instance:
Application excel = new Application();
// open the concrete file:
Workbook excelWorkbook = excel.Workbooks.Open(@filePath);
// select worksheet. NOT zero-based!!:
_Worksheet excelWorkbookWorksheet = excelWorkbook.ActiveSheet;
if(isclosing)
{
closeProgram(excel, excelWorkbook);
}
int numRows = excelWorkbookWorksheet.UsedRange.Rows.Count;
Console.WriteLine("Number of rows: " + numRows);
Console.Write("Checking Row #: " + currRowNumber);
int numRowsDeleted = 0;
int nullCounter = 0;
//for (int j = 1; j <=)
for (int i = 1; i < numRows + 4; i++)
{
//We want to skip every row that is null and continue looping until we have more than 3 rows in a row that are null, then break
if (i > 1)
{
i -= 1;
}
//Create Worksheet Range
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)excelWorkbookWorksheet.Cells[i, 2];
string cellValue = Convert.ToString(range.Value);
if (nullCounter == 5)
{
Console.WriteLine("Null row detected...breaking");
Console.WriteLine("Number of rows deleted: " + numRowsDeleted);
break;
}
if (cellValue != null)
{
if (cellValue.Contains("MESSAGE NOT CONFIGURED"))
{
//Console.WriteLine("Deleting Row: " + Convert.ToString(cellValue));
((Range)excelWorkbookWorksheet.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp);
numRowsDeleted++;
//Console.WriteLine("Number of rows deleted: " + numRowsDeleted);
nullCounter = 0;
i--;
currRowNumber++;
}
else
{
currRowNumber++;
nullCounter = 0;
}
}
else
{
nullCounter++;
//Console.WriteLine("NullCounter: " + nullCounter);
}
i++;
}
Console.WriteLine("Fixes Finished! Please check your excel file for correctness");
closeProgram(excel, excelWorkbook);
}
Sample output:
Row Number: 1
Row Number: 2
Row Number: 3
Row Number: 4
Row Number: 5
etc..
I want it to display only one line and continuously update the row number. How would i go about doing this? Any help is appreciated. Thanks.
UPDATE: So i have the following loop:
for (int i = 1; i < numRows + 2; i++) //numRows was +4, now +2
{
Console.Clear();
Console.WriteLine("Number of rows: " + numRows);
Console.Write("Checking Row #: " + currRowNumber);
//We want to skip every row that is null and continue looping until we have more than 3 rows in a row that are null, then break
if (i > 1)
{
i -= 1;
}
//Create Worksheet Range
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)excelWorkbookWorksheet.Cells[i, 2];
string cellValue = Convert.ToString(range.Value);
if (nullCounter == 3) //was 5
{
Console.WriteLine("\nNull row detected...breaking");
Console.WriteLine("Number of rows deleted: " + numRowsDeleted);
break;
}
if (cellValue != null)
{
if (cellValue.Contains(searchText))
{
//Console.WriteLine("Deleting Row: " + Convert.ToString(cellValue));
((Range)excelWorkbookWorksheet.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp);
numRowsDeleted++;
//Console.WriteLine("Number of rows deleted: " + numRowsDeleted);
nullCounter = 0;
i--;
currRowNumber++;
rowsPerSecond = i;
}
else
{
currRowNumber++;
nullCounter = 0;
}
}
else
{
nullCounter++;
//Console.WriteLine("NullCounter: " + nullCounter);
}
i++;
}
I want to calculate how many rows im looping through per second, then calculate from that number how long it will take to complete the entire loop, based on how many rows there are.
Again, any help is appreciated, thanks!