2016-11-22 38 views
0

我想要制作一个循环,通过Excel表格中的特定列号的所有行,比如列号为16,对每行的每个单元格的值做一些处理。 例如,循环将遍历单元格1,16,然后下一个单元格到2,16,然后是下一个3,16 ....然后一直走到该表单在该特定列中具有的许多行号,在这种情况下,列数16 到目前为止,我能够获取和设置使用语句的一个Excel单元格的值,如本:在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; 

有什么想法?

回答

2

您需要这里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; 
} 
+0

作品,非常感谢! –

0

我假设你正在使用C#和COM的Microsoft.Office.Interop.Excel +库。

尝试像

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; 
} 
相关问题