【问题标题】:Iterate all rows of a specific column number in excel sheet and do some processing on each row value迭代excel工作表中特定列号的所有行并对每一行值进行一些处理
【发布时间】:2017-09-30 00:34:12
【问题描述】:

我正在尝试创建一个循环,遍历 Excel 工作表中特定列号的所有行,例如列号 16,并对每行中每个单元格的值进行一些处理。 例如,循环将遍历单元格 1,16,然后下一个到单元格 2,16,然后下一个 3,16....然后一直到工作表在该特定列号中的行数,在这种情况下,第 16 列。 到目前为止,我能够使用如下语句获取和设置单个 excel 单元格的值:

string cellValue = excelSheet.Cells[1, 16].Value.ToString();
//Do some processing.
excelSheet.Cells[1, 16] = cellValue; 

但我想在我的循环中将行号循环到类似这样的音调:

string cellValue = excelSheet.Cells[n, 16].Value.ToString();
//Do some processing.
excelSheet.Cells[n, 16] = cellValue; 

有什么想法吗?

【问题讨论】:

    标签: c# .net excel


    【解决方案1】:

    您需要for loop

    for(int n = 1; n <= excelSheet.Columns.Count; n++)
    {
        string cellValue = excelSheet.Cells[n, 16].Value.ToString();
        //Do some processing.
        excelSheet.Cells[n, 16] = cellValue; 
    }
    

    【讨论】:

      【解决方案2】:

      我假设您使用的是 C# 和 Microsoft.Office.Interop.Excel COM+ 库。

      试试类似的东西

      Microsoft.Office.Interop.Excel.Range range = excelSheet.UsedRange;
      for (int index = 0; index < range.Rows.Count; index++)
      {
          string cellValue = excelSheet.Cells[index, 16].Value.ToString();
          //Do some processing.
          excelSheet.Cells[index, 16] = cellValue; 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-23
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        相关资源
        最近更新 更多