Java 不兼容的操作数类型int和CellType
因此,我尝试使用Java代码阅读excel工作表。代码如下:Java 不兼容的操作数类型int和CellType,java,excel,apache-poi,Java,Excel,Apache Poi,因此,我尝试使用Java代码阅读excel工作表。代码如下: package com.SfHawk.Utility; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; i
package com.SfHawk.Utility;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExcelApiTest {
public FileInputStream fis = null;
public XSSFWorkbook workbook = null;
public XSSFSheet sheet = null;
public XSSFRow row = null;
public XSSFCell cell = null;
public ExcelApiTest(String xlFilePath) throws Exception
{
fis = new FileInputStream(xlFilePath);
workbook = new XSSFWorkbook(fis);
fis.close();
}
public String getCellData(String sheetName, String colName, int rowNum)
{
try
{
int col_Num = -1;
sheet = workbook.getSheet(sheetName);
row = sheet.getRow(0);
for(int i = 0; i < row.getLastCellNum(); i++)
{
if(row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
col_Num = i;
}
row = sheet.getRow(rowNum - 1);
cell = row.getCell(col_Num);
if(cell.getCellType() == CellType.STRING)
return cell.getStringCellValue();
else if(cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
{
String cellValue = String.valueOf(cell.getNumericCellValue());
if(HSSFDateUtil.isCellDateFormatted(cell))
{
DateFormat df = new SimpleDateFormat("dd/MM/yy");
Date date = cell.getDateCellValue();
cellValue = df.format(date);
}
return cellValue;
}else if(cell.getCellType() == CellType.BLANK)
return "";
else
return String.valueOf(cell.getBooleanCellValue());
}
catch(Exception e)
{
e.printStackTrace();
return "row "+rowNum+" or column "+colNum +" does not exist in Excel";
}
}
}
package com.SfHawk.Utility;
导入org.apache.poi.hssf.usermodel.HSSFDateUtil;
导入org.apache.poi.ss.usermodel.CellType;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入java.io.FileInputStream;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.util.Date;
公开课卓越测验{
public FileInputStream fis=null;
公共XSSFWorkbook工作簿=空;
公共XSSFSheet=null;
public XSSFRow row=null;
公共XSSFCell单元=null;
公共ExcelApiest(字符串xlFilePath)引发异常
{
fis=新文件输入流(xlFilePath);
工作簿=新XSSF工作簿(fis);
fis.close();
}
公共字符串getCellData(字符串sheetName、字符串colName、int rowNum)
{
尝试
{
int col_Num=-1;
sheet=工作簿.getSheet(sheetName);
行=表。getRow(0);
对于(int i=0;i
上面的代码给了我一个错误:
不兼容的操作数类型int和CellType
最初我使用cell.getCellTypeEnum()方法代替cell.getCellType()。但是cell.getCellTypeEnum()给我的错误是,类型XSSFCell的getCellTypeEnum()方法未定义
如何解决此问题
提前感谢。您使用的是什么
ApachePOI
版本?并且类路径中没有不同版本的apachepoijar
s。例如,不要同时在类路径中包含poi-3.17.jar
和poi-ooxml-4.1.2.jar
和poi-4.1.2.jar
和poi-ooxml-3.17.jar
。感谢您的快速回复。下面是我在pom.xml中的依赖项。org.apache.poi poi 3.12 org.apache.poi ooxml 3.9如上所述,不要混合使用不同的apache poi
版本。您正在混合3.12
和3.9
。但两者都是古老而过时的。请使用一个当前版本执行此操作。