Getting Cell Contents

Aspose.Cells - Getting Cell Contents

The Cells.get() method is available to access cells.

Java

// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();

// Access the maximum display range
Range range = worksheet.getCells().getMaxDisplayRange();
int tcols = range.getColumnCount();
int trows = range.getRowCount();

System.out.println("Total Rows:" + trows);
System.out.println("Total Cols:" + tcols);

// Access the value of cell B4
//=====================================================
System.out.println(cells.get("B4").getValue());

Cell cell = cells.get(3, 1); // Access the value of cell B4
System.out.println(cell.getValue());
//=====================================================

RowCollection rows = cells.getRows();
for (int i = 0; i < rows.getCount(); i++) {
    for (int j = 0; j < tcols; j++) {
        if (cells.get(i, j).getType() != CellValueType.IS_NULL) {
            System.out.println(cells.get(i, j).getName() + " - " + cells.get(i, j).getValue());
        }
    }
}

Apache POI SS - HSSF XSSF - Getting Cell Contents

Apache POI provides the Cell class to access various properties of cells.

Java

Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
    for (Cell cell : row) {
        CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
        System.out.print(cellRef.formatAsString());
        System.out.print(" - ");
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                System.out.println(cell.getRichStringCellValue().getString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.println(cell.getDateCellValue());
                } else {
                    System.out.println(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                System.out.println(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                System.out.println(cell.getCellFormula());
                break;
            default:
                System.out.println();
        }
    }
}

Download Running Code

Download Sample Code