在ListObject for循环中打印和使用Var

本文关键字:Var 打印 ListObject for 循环 | 更新日期: 2023-09-27 18:15:27

我目前有这个c#代码可以访问table1。我有一个问题,但Var listRowsValue和Var lr。我怎么能打印这些并减去1的ListRowsValue,而不是查看"系统。_ComObject"作为输出?我如何调整ListObject的大小,以不检查计数结束时的另一行?

var listRowsValue = xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].ListRows;
for (int CurrentRow = 0; CurrentRow < listRowsValue.Count; CurrentRow++)
{
    if (xlWorkBook.Worksheets["Sheet1"].ListObjects["table1"].Range[CurrentRow, 2].value == -1)
    {
        xlSheet.Cells[5, 5] = "YES!";
        //lr.Range.Clear();   
        var lr = listRowsValue[CurrentRow];
        Console.WriteLine("CURRENT ROW: " + lr);//Why does this not work?       
        //MoveEmUpOne();
        //How do I resize the total listRowsValue count here so it doesn't check another row at the end?
        //EXAMPLE: ListRowsValue = ListRowsValue - 1;
        //CurrentRow = CurrentRow - 1;
    }
    else
    {
         xlSheet.Cells[5, 5] = "NO!";
    }
}

我正在转换的旧VBA代码:

For CurrentRow = 1 To Tablesize
    If Lo.ListColumns("Column2").DataBodyRange(CurrentRow) = -1 Then
        Ros(CurrentRow).Range.Clear
        MoveEmUpOne Lo, CurrentRow, Tablesize
        Tablesize = Tablesize - 1 'table didn't really get smaller, but number of "real"rows decreased by 1
        CurrentRow = CurrentRow - 1 ' check this row again -- stuff from below might need cleard
    End If

在ListObject for循环中打印和使用Var

VSTO的用法与普通的c#库有很大的不同,因为它主要依赖于COM。

lr是ListRow对象。

使用lr.Range.Text获取其中的文本。

Console.WriteLine("CURRENT ROW: " + lr.Range.Text);

参见Range.Text