We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Column length

Hi,

Good day to you. I have a two fold problem. Firstly I would like to be able to retrieve the length of a specific column (eg say I have 6 columns, I would like to know the length of cells that have data in them).

Secondly as I would like to be able to programmatically access the data in cells within a specific column and then add them to a list object.

I have tried the following code but it tells me that columns do not have enumerators to use foreach loops

List myList = new List();

Worksheet wksheet = this.StockWorkBook.Worksheets[0];
Aspose.Cells.Column col;
col = wksheet.Cells.Columns[4];

foreach (Aspose.Cells.Cell cell in col)
{
MyList.Add(Convert.ToDecimal(cell.Value.ToString()));
}

Many thanks


This message was posted using Aspose.Live 2 Forum

Hi,

1) To get the last column index which has data in it, please use Cells.MaxDataColumn attribute.
To get the last row index which has data in it, please use Cells.MaxDataRow attribute.

2) I think you may change your code a bit.

Workbook wb = new Workbook();
Worksheet worksheet = wb.Worksheets[0];
Cells cells = worksheet.Cells;

for (int i = 0; i < 22; i++)
{
cells[i, 4].PutValue(i);

}

List myList = new List();

int last_row = worksheet.Cells.GetLastDataRow(4);

for(int i = 0;i<=last_row;i++)
{
myList.Add(Convert.ToDecimal(cells[i,4].Value.ToString()));
}


Thank you.

Thanks.

With regards to retrieving a count of cells in a column on a worksheet how do I specify a specific column (eg out of 6 seperate different length columns, I only wish to find the length of column number 4), maxdatacolumn doesn’t allow me to specify an index, and if I create a Aspose.Cells.Column object and assign the worksheet column to it it has no method for returning its length/count.

Its this column length that I then wish to use in the FOR loop above.


Kind regards

Hi,

Could you check my code:
int last_row = worksheet.Cells.GetLastDataRow(4);

The line would give you the last row index in the fifth column i.e. “E”. Cells.GetLastDataRow() method takes the argument of column index for which you wish to check the length, the method would give you the last cell’s row index (zero based), e.g if it gives you 21, it means there are total 22 cells in that column.

Thank you.