java - 从 Excel 文件的单元格获取值不起作用

标签 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

最佳答案

您的导入错误。使用:

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

而不是:

import jxl.Cell;
import jxl.Sheet;

关于java - 从 Excel 文件的单元格获取值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20611995/

相关文章:

java - FirebaseUI-无法识别新用户

excel - 如果 VBA.CVErr() 不能支持 vbObjectError 的 Long vartype..,应该如何使用它?

python - 将 Excel 命名范围读入 pandas DataFrame

excel - VBA 最长 Collat​​z 序列

excel - 试图从 "Main"表自动添加超链接到新的自动化表?

excel - 如何让 Msgbox 有选择地出现或不出现

java - 如何使用 spring 3.2 新 mvc 测试登录用户

java - Oracle 的服务器 JRE 包含 JDK?

java - 尝试使用 DataflowRunner 时出现 ClassNotFound 异常

java 。全局JLabels