C#在控制台中显示单行循环迭代

时间:2017-06-30 12:06:43

标签: c# loops for-loop

所以我使用for循环遍历excel电子表格中的所有行,我只需使用" i"就可以非常轻松地显示当前行号。定义,但它打印在多行上,因为每个iteraton都显示一个Console.WriteLine()命令。

我想要的是它只展示一次,并让它在一行上显示更新的迭代。这是我目前的代码:

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

示例输出:

Row Number: 1
Row Number: 2
Row Number: 3
Row Number: 4
Row Number: 5

等。

我希望它只显示一行并不断更新行号。我该怎么做呢?任何帮助表示赞赏。感谢。

更新: 所以我有以下循环:

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++;

        }

我想计算每秒循环的行数,然后根据该数量计算完成整个循环所需的时间,具体取决于有多少行。

再一次,感谢任何帮助,谢谢!

6 个答案:

答案 0 :(得分:0)

在每个循环圈上,在打印输出之前使用Clear()方法:

Console.Clear();

答案 1 :(得分:0)

你可以这样做:

const int ConsoleWidth = 80;
for(int i = 0; i < 10; i++)
{
    // pull cursor to the start of the line, delete it with spaces
    // then pull it back again
    for(int j = 0; j < ConsoleWidth; j++) Console.Write("\b");
    for(int j = 0; j < ConsoleWidth; j++) Console.Write(" ");
    for(int j = 0; j < ConsoleWidth; j++) Console.Write("\b");

    Console.Write("Row Number: {0}", i);

}

答案 2 :(得分:0)

如果你这样做

Console.Write("Row Number: {0}", i);

它将继续附加在前一个文本的前面。 如果你这样做

Console.Clear();

在本文之前您打算写的任何消息都将消失。 如果您知道文本的确切位置,则可以尝试将控制台中的文本修改为:

// First time write
Console.WriteLine("Row Number: {0}", i);

// x,y coordinates in the method for later iterations
Console.SetCursorPosition(11, 0);
Console.Write(i);

答案 3 :(得分:0)

如果行中的文字数量永远不会减少(例如,因为数字只会变大),您可以使用Console.Write()并在\r前加上文字前缀:

for (int i = 0; i < 100000; ++i)
    Console.Write($"\rRow Number: {i}");

Console.WriteLine();

如果文本因长度减小而缩短,则可以使用格式化字符串输出左对齐的数字,并使用额外的空格覆盖额外的数字:

for (int i = 100000; i >= 0; --i)
    Console.Write($"\rRow Number: {i, -6}");

Console.WriteLine();

这样做的好处是可以在控制台中保留以前的任何文本,只更新单行。与每次迭代清除整个显示相比,这也使得它更快。

答案 4 :(得分:0)

你可以像之前的答案那样做,这是另一种方式。

 int line = Console.CursorTop;
 int len = new String("Row Number: ").length;
 Console.WriteLine("Row Number: ");
 for(your loop)
 {
  Console.WriteLine(i);
  //Print whatever else you want!
  Console.SetCursorPosition(len,line);
 }

这种方法的优点在于记住位置,并且在向用户显示任何其他信息后可以回到相同的位置。这在更新充满文本的屏幕中的单个字符时很有用!

要回答下一个问题,请使用

Stopwatch s = new Stopwatch();
long millis =0,avg;
for(int i=0;i<loopLength;i++)
{
 s.Start();
 //Do work
 s.Stop();
 millis += s.ElapsedMilliseconds;
avg = millis/(i+1);
 Console.WriteLine("Time remaining :" + (TimeSpan.FromMilliseconds(avg*(loopLength - (i+1))).Seconds);
}

答案 5 :(得分:0)

尝试在Console.Write之前使用Console.SetCursorPosition