猿问

CELL_TYPE_STRING 无法解析或不是字段

堆栈跟踪


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)

代码


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;

                }

            }


        }


    }


}


小唯快跑啊
浏览 1144回答 3
3回答

慕雪6442864

该enums为STRING,NUMERIC并BOOLEAN,降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;}

SMILET

如果您使用更高版本的 Apache POI poi-4.0.1,则Cell.getCellType()返回 CellType 枚举而不是 int,因此您的开关应如下所示:现在在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;                    }

繁花如伊

试试这个代码。它有效...您只需要知道 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 {&nbsp; &nbsp; public static void main(String[] args) throws Exception{&nbsp; &nbsp; &nbsp; &nbsp; ArrayList data = new ArrayList();&nbsp; &nbsp; &nbsp; &nbsp; FileInputStream file = new FileInputStream("F://LeadSuite.xlsx");&nbsp; &nbsp; &nbsp; &nbsp; XSSFWorkbook book = new XSSFWorkbook(file);&nbsp; &nbsp; &nbsp; &nbsp; XSSFSheet s = book.getSheet("TestSteps");&nbsp; &nbsp; Iterator itr = s.iterator();&nbsp; &nbsp; while (itr.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; Row rowitr = (Row) itr.next();&nbsp; &nbsp; &nbsp; &nbsp; Iterator cellitr = rowitr.cellIterator();&nbsp; &nbsp; &nbsp; &nbsp; while(cellitr.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cell celldata = (Cell) cellitr.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch(celldata.getCellType()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case STRING:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.add(celldata.getStringCellValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case NUMERIC:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.add(celldata.getNumericCellValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case BOOLEAN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.add(celldata.getBooleanCellValue());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for (int i=0;i<data.size();i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(data.get(i).equals("Sharan")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+3));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(data.get(i).equals("Kiran")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+3));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(data.get(i).equals("Jhade")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(data.get(i+3));}}&nbsp; &nbsp; &nbsp; &nbsp;}}
随时随地看视频慕课网APP

相关分类

Java
我要回答