Java CELL_TYPE_STRING 无法解析或不是字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52514468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:29:38  来源:igfitidea点击:

CELL_TYPE_STRING cannot be resolved or is not a field

javaapache-poi

提问by lenin kumam

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

        }

    }

}

回答by Guy

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

enums为STRINGNUMERICBOOLEAN,降CELL_TYPE_,他们的部分单元格类型枚举

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

回答by Abderrahmen

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:

如果您使用更高版本的 Apache POI poi-4.0.1,则Cell.getCellType()返回 CellType 枚举而不是 int,因此您的开关应如下所示:

now in CELL_TYPE_NUMERICis now just NUMERICremove CELL_TYPE_

现在在CELL_TYPE_NUMERIC现在只是NUMERIC删除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;
                    }

回答by Sai Sharan

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

试试这个代码。它有效...您只需要知道 poi api 版本并遵循 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));
}
}       
}
}