lenin kumam
lenin kumam

Reputation: 121

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

        }

    }

}

Upvotes: 12

Views: 73618

Answers (5)

mailtime
mailtime

Reputation: 151

JDK 15

switch (cell.getCellType()) {
              case NUMERIC -> data.add(cell.getNumericCellValue());
              case STRING ->  data.add(cell.getStringCellValue());
                }

Upvotes: 0

Sudheer Singh
Sudheer Singh

Reputation: 654

Use switch-case like this

              switch (cell.getCellType()) {
              case STRING:
                System.out.print(cell.getStringCellValue() + "\t\t\t");
                break;
              case NUMERIC:
                System.out.print(cell.getNumericCellValue() + "\t\t\t");
                break;
              default:
            }

Upvotes: 2

Sai Sharan
Sai Sharan

Reputation: 53

Try this Code. It works... You just need to know poi api version and follow the new changes in poi api.

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.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class A {

    public static void main(String[] args) throws Exception{

        ArrayList data = new ArrayList();

        FileInputStream file = new FileInputStream("F://LeadSuite.xlsx");
        XSSFWorkbook book = new XSSFWorkbook(file);
        XSSFSheet s = book.getSheet("TestSteps");

    Iterator itr = s.iterator();
    while (itr.hasNext()) {
        Row rowitr = (Row) itr.next();
        Iterator cellitr = rowitr.cellIterator();
        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;
            }
        }
    }

    for (int i=0;i<data.size();i++) {
        if(data.get(i).equals("Sharan")) {
            System.out.println(data.get(i));
            System.out.println(data.get(i+1));
            System.out.println(data.get(i+2));
            System.out.println(data.get(i+3));          
        }
        if(data.get(i).equals("Kiran")) {
            System.out.println(data.get(i));
            System.out.println(data.get(i+1));
            System.out.println(data.get(i+2));
            System.out.println(data.get(i+3));
        }
        if(data.get(i).equals("Jhade")) {
            System.out.println(data.get(i));
            System.out.println(data.get(i+1));
            System.out.println(data.get(i+2));
            System.out.println(data.get(i+3));
}
}       
}
}

Upvotes: 4

Abdo Bmz
Abdo Bmz

Reputation: 641

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

Upvotes: 26

Guy
Guy

Reputation: 50809

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

Upvotes: 25

Related Questions