Reputation: 794
I am trying to make a loop that goes though all rows of a specific column number in an excel sheet, say column number 16, and do some processing on the values of each cell in each row. For example, the loop will iterate though cells 1,16, then next one goes to cell 2,16, then next one 3,16....then goes all the way to however many rows that sheet has in that specific column number, in this case, column number 16. So far I am able to get and set the value of a single excel cell using statements such as this:
string cellValue = excelSheet.Cells[1, 16].Value.ToString();
//Do some processing.
excelSheet.Cells[1, 16] = cellValue;
But I want to loop though the row number to the tone of something like this inside my loop:
string cellValue = excelSheet.Cells[n, 16].Value.ToString();
//Do some processing.
excelSheet.Cells[n, 16] = cellValue;
Any thoughts?
Upvotes: 0
Views: 4876
Reputation: 14477
You need a for loop
here:
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;
}
Upvotes: 2
Reputation: 46
I'm assuming you're using C# and Microsoft.Office.Interop.Excel COM+ library.
Try something like
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;
}
Upvotes: 2