How to iterate a named column (example: foreach .... column["A"])

Hi,

Is there a way to iterate a whole column (to find if each cell in that column has some data)?

something like:
foreach (string cell in worksheet.Cells.Columns[“A”])

alternatively, is there a way to easily validate a whole column to find if any cells are empty?

Hi,


I have written an example code for your reference. I used a simple Excel file for it. It will iterate the whole A column and scan for values or empty cells, it will print the values and also give the names for the blank or empty cells in the first worksheet of the workbook.

See the following sample code for your needs.

Sample code:

Workbook workbook = new Workbook(“e:\test2\Book1.xls”);
Worksheet sheet = workbook.Worksheets[“Sheet1”];
Cells cells = sheet.Cells;

//Get the last row index in the column for the loop iteration
int maxrow = cells.GetLastDataRow(CellsHelper.ColumnNameToIndex(“A”));
string val1;
for (int i = 0; i <= maxrow; i++)
{
Aspose.Cells.Cell cell = cells[i, CellsHelper.ColumnNameToIndex(“A”)];

if (cell != null && cell.Type != CellValueType.IsNull)
{
string cellValue = cell.StringValue;
Console.WriteLine(cell.StringValue);

}
else
{
// Blank cells
Console.WriteLine ("Blank Cell Name: " + cell.Name + " Value: " + cell.Value);
}

}

Hope, this helps.

Thank you.