迭代Excel表中特定列号的所有行,并对每个行值进行一些处理

我试图做一个循环,通过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; 

有什么想法吗?

这里需要一个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; } 

我假设你正在使用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; }