Menu

Getting Cell Contents

Shoaib

Apache POI SS - Getting Cell Contents

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();
        }
    }
}

Aspose.Cells

//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 value of Cell B4
//=====================================================
System.out.println(cells.get("B4").getValue());

Cell cell = cells.get(3,1); //Access 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());
        }
    }
}

Download Source Code


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.