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

时间:2013-12-16 13:21:05

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

3 个答案:

答案 0 :(得分:1)

导入错误。使用方法:

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

而不是:

import jxl.Cell;
import jxl.Sheet;

答案 1 :(得分:1)

JXL(现在是'JExcel')和Apache POI是两个独立的excel apis,可供Java开发人员与microsoft excel连接。

你肯定不会期望这两者彼此兼容,这就是你得到类型不匹配错误的原因。

(您应该只使用POI或仅使用JXL) 对于POI,您可以导入

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

并将您的代码更改为

HSSFSheet sheet1 = wb.getSheetAt(0);  

答案 2 :(得分:0)

我认为您在将大小写更改为:

时遇到问题
  switch (cell.getCellType()) {
            case STRING:
                System.out.println(cell.getRichStringCellValue().getString());
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.println(cell.getDateCellValue());
                } else {
                    System.out.println(cell.getNumericCellValue());
                }
                break;
            case BOOLEAN:
                System.out.println(cell.getBooleanCellValue());
                break;
            case FORMULA:
                System.out.println(cell.getCellFormula());
                break;
            default:
                System.out.println();
            }
相关问题