我正在使用以下代碼,在「獲取單元格內容」標題下的「http://poi.apache.org/spreadsheet/quick-guide.html#Iterator」上閱讀。即使在編譯之前,它也會出現很多錯誤。我在最後提到了錯誤。我想要做的是將excel表單中的單元格的內容與其Web應用中字段的內容進行比較,該字段的ID可以說是「label22」獲取Excell文件的單元格中的值不起作用
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
? – TheLostMind
讓我編輯它。 – newLearner