Read values of cells in a column from Row X to end of Column?

Tried searching for how to do this. I’d think it’d be sort of common but couldn’t find it while searching…


I’m using aspose cells to allow the user to upload an excel doc. I then need to grab the values from each column from say row 4 to the end of the column.

What’s the cleanest way to do this?
I know I can get the last column and then beginning iterating over my columns:

int lastColumn = cells.getRows().get(0).getLastCell().getColumn();
for(int col=0;col <= lastColumn;col++) {
Column column = cells.getColumns().get(col);

But now I can’t seem to do much with a Column object. I thought they’d be a way on a Column to get the last cell so that you could get a list of Cells to iterate over… something like…

CellCollection cells = column.get(startIndex, column.getLastCell() )

What’s the best way to achieve what I want to do (for each column I want to look at the values in each of the cells from row x to the end of the column - note the column heights could vary… they might not all have the same number of values.)

Thanks

Hi,

Thanks for your question and using Aspose.Cells for Java.

Please use the Cell.endCellInColumn() method, it will return you the last cell in a column, so you will then know the column height.

From the cell, you can use the Cell.getRow() and Cell.getColumn() property to retrieve cell’s exact position based on rows and columns.

Please see the code below and its output. I have also attached the source xlsx file used in this code.

Java


String filePath = “F:\Shak-Data-RW\Downloads\source.xlsx”;


//Create a workbook from source file

Workbook workbook = new Workbook(filePath);


//Access the first worksheet

Worksheet worksheet = workbook.getWorksheets().get(0);


//Read all the cells in column C


//Column C

int column = 2;


//Get last cell in column C

Cell lastCell = worksheet.getCells().endCellInColumn((short)column);


for(int row =0; row<=lastCell.getRow(); row++)

{

Cell cell = worksheet.getCells().get(row, column);


System.out.println(cell.getStringValue());

}


Output:
5
3
2
3
1
1
3
1
4
5

Hi,


And, here is the simplest sample code that can help you for your reference.

Sample code:

Workbook workbook = new Workbook(“e:\test\Book1.xls”);
Cells cells = workbook.getWorksheets().get(0).getCells();

//Get the last row index in the column "A"
int lastrow = cells.endCellInColumn((short)0).getRow();

//Print all the values in the first column "A"
for (int i = 0; i < lastrow; i++)
{
System.out.println(cells.get(i,0).getValue());

}