0

I have a column (B) that I need to take all the values between B3 and B20

this is my code

try {
            OPCPackage fs;

            fs =  OPCPackage.open(new File(getFilePath()));

            XSSFWorkbook wb = new XSSFWorkbook(fs);
            XSSFSheet sheet = wb.getSheet("Master column name - Used Car");
            XSSFRow row;
            CellReference cr = new CellReference("B3");

            row = sheet.getRow(4); 
            System.out.println(row);

but as you see, i am getting one value, i didn't know how to get the values for cells B3 until B20

could you help please

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

2 Answers2

1

have you tried replacing this line:

CellReference cr = new CellReference("B3");

with:

AreaReference ar = new AreaReference("B3:B20");

i.e.

AreaReference ar = new AreaReference("B3:B20");

for (cr : ar.getAllReferencedCells()) {
    System.out.print(cr.formatAsString());
    System.out.print(" - ");
}
System.out.println();
micsthepick
  • 562
  • 7
  • 23
1

To read values of certain column or cell from excel you might try this

public static void readFromExcel2(){
    try{
        FileInputStream file = new FileInputStream(new File("java_excel.xlsx"));//place path of your excel file
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(1);//which sheet you want to read

        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()){
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext()){

                Cell cell = cellIterator.next();
                if(cell.getColumnIndex()<2&&(cell.getRowIndex()>=3&&cell.getRowIndex()<=20)) {
                     {

                        switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_NUMERIC:
                                System.out.print((int) cell.getNumericCellValue()+" \t" );
                                break;
                            case Cell.CELL_TYPE_BLANK:
                                System.out.print(" ");
                                break;
                            case Cell.CELL_TYPE_STRING: {
                                System.out.print(cell.getStringCellValue());
                            }

                        }
                    }
                }
            }
            System.out.println(" ");
        }
        file.close();
    }catch (Exception e){
        e.printStackTrace();
    }
}

}

Jessie.Y
  • 11
  • 2