Free Support Forum - aspose.com

How to loop cells

Hi, how can I loop through all the rows and columns of a xls spreadsheet?

Can you help me?

Hi,

Thanks for considering Aspose.

You mean, you want to loop through all the cells with data in rows/columns in a worksheet of your template file. Well, you may use MaxDataRow and MaxDataColumn properties of the Cells class.

(Sample code) E.g.,

Workbook workbook = new Workbook();
workbook.Open("d:\\test\\testbook.xls");
Worksheet ws = workbook.Worksheets[0];
for (int row=0;row<=ws.Cells.MaxDataRow;row++)
{
for (int col =0;col<= ws.Cells.MaxDataColumn; col++ )
{
//Your code goes here........
if (ws.Cells[row,col].StringValue == "mydata")
{
ws.Cells[row,col].PutValue("newvalue");
break;
}

}
}

Thank you.