Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CELL_TYPE_STRING cannot be resolved or is not a field

Stack trace

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
CELL_TYPE_STRING cannot be resolved or is not a field
CELL_TYPE_NUMERIC cannot be resolved or is not a field
CELL_TYPE_BOOLEAN cannot be resolved or is not a field

at len.a.main(a.java:30)

Code

package len;

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class a {

    public static void main(String[] args) throws Exception{
        ArrayList data =new ArrayList();
        FileInputStream f =new FileInputStream("E:\\leadsuite.xlsx");
        XSSFWorkbook wb=new XSSFWorkbook(f);
        Sheet s=wb.getSheet("test steps");
        Iterator itr=s.iterator();
        while(itr.hasNext())
        {
            Row r=(Row) itr.next();
            Iterator cellitr=r.cellIterator();
            while(cellitr.hasNext())
            {
                Cell celldata=(Cell) cellitr.next();
                switch(celldata.getCellType())
                {
                case Cell.CELL_TYPE_STRING:
                    data.add(celldata.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    data.add(celldata.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    data.add(celldata.getBooleanCellValue());
                    break;
                }
            }

        }

    }

}
like image 245
lenin kumam Avatar asked Sep 26 '18 09:09

lenin kumam


2 Answers

If you're using a later version of Apache POI poi-4.0.1, Cell.getCellType() returns the CellType enum instead of int, so your switch should look like this:

now in CELL_TYPE_NUMERIC is now just NUMERIC remove CELL_TYPE_

  while(cellitr.hasNext())
                {
                    Cell celldata=(Cell) cellitr.next();
                    switch(celldata.getCellType())
                    {
                    case STRING:
                        data.add(celldata.getStringCellValue());
                        break;
                    case NUMERIC:
                        data.add(celldata.getNumericCellValue());
                        break;
                    case BOOLEAN:
                        data.add(celldata.getBooleanCellValue());
                        break;
                    }
like image 50
Abdo Bmz Avatar answered Sep 23 '22 15:09

Abdo Bmz


The enums are STRING, NUMERIC and BOOLEAN, drop the CELL_TYPE_, and they are part of CellType enum

switch(celldata.getCellType()) {
    case CellType.STRING:
        data.add(celldata.getStringCellValue());
        break;
    case CellType.NUMERIC:
        data.add(celldata.getNumericCellValue());
        break;
    case CellType.BOOLEAN:
        data.add(celldata.getBooleanCellValue());
        break;
}
like image 33
Guy Avatar answered Sep 20 '22 15:09

Guy