java从Excel文件的单元格中获取值不起作用
我使用的是我在“获取单元格内容”标题下的“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
# 1 楼答案
你的进口错了。使用:
而不是:
# 2 楼答案
我认为你有问题把案子改成:
# 3 楼答案
JXL(现在的“JExcel”)和Apache POI是两个独立的excel API,java开发人员可以使用它们与microsoft excel进行交互
您当然不能期望这两种类型彼此兼容,这就是为什么会出现类型不匹配错误的原因
(应仅使用POI或JXL) 对于POI,您可以导入
并将代码更改为