3 回答
TA贡献1812条经验 获得超5个赞
该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;
}
TA贡献1796条经验 获得超4个赞
如果您使用更高版本的 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;
}
TA贡献2012条经验 获得超12个赞
试试这个代码。它有效...您只需要知道 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));
}
}
}
}
添加回答
举报