从Excell文件的单元格获取值不起作用

我正在使用下面的代码,在“获取单元格内容”标题下的“ http://poi.apache.org/spreadsheet/quick-guide.html#Iterator ”上阅读。 即使在编译之前,它也会出现很多错误。 我在最后提到了错误。我想要做的是取出excel表格的单元格的内容,并将其值与我的web应用程序中的字段内容进行比较,该字段的ID为“label22”

import java.io.FileInputStream; import java.io.InputStream; import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellReference; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; public class ReadCell { public static void main(String[] args) { InputStream inp = new FileInputStream("D:\\rptViewer.xls"); HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp)); ExcelExtractor extractor = new ExcelExtractor(wb); extractor.setFormulasNotResults(true); extractor.setIncludeSheetNames(false); String text = extractor.getText(); /* * Read excel file using j excel file */ Sheet sheet1 = wb.getSheetAt(0); for (Row row : sheet1) { for (Cell cell : row) { CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); System.out.print(cellRef.formatAsString()); System.out.print(" - "); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println(cell.getRichStringCellValue().getString()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.println(cell.getDateCellValue()); } else { System.out.println(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: System.out.println(cell.getCellFormula()); break; default: System.out.println(); } } } } 

错误是:

 getColumnIndex : The method getColumnIndex() is undefined for the type Cell wb.getSheetAt(0): Type mismatch: cannot convert from HSSFSheet to Sheet formatAsString()); : The method formatAsString() is undefined for the type CellReference getCellType()) { :The method getCellType() is undefined for the type Cell getRichStringCellValue() : The method getRichStringCellValue() is undefined for the type Cell CELL_TYPE_NUMERIC: : CELL_TYPE_NUMERIC cannot be resolved or is not a field 

你的import是错的。 使用:

 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; 

代替:

 import jxl.Cell; import jxl.Sheet; 

JXL (现在称为“JExcel”)和Apache POI是两个独立的,可供java开发人员用于与Microsoft Excel进行交互的excel应用程序。

你当然不会期望这两个是相互兼容的,这就是为什么你得到types不匹配的错误。

(您应该只使用兴趣点或只有JXL)您可以导入POI

 import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFCell; 

并将您的代码更改为

 HSSFSheet sheet1 = wb.getSheetAt(0);