STRING无法解析或不是字段

时间:2018-04-12 09:34:53

标签: java

错误: 线程“main”中的异常java.lang.Error:未解决的编译问题: STRING无法解析或不是字段,NUMERIC无法解析或不是字段,BOOLEAN无法解析或不是字段,BLANK无法解析或不是字段。我正在使用下面的代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator; 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {

    public static void main(String[] args) throws Exception {
        String filename = "test.xlsx";

        try (FileInputStream fis = new FileInputStream(filename)) {
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            HSSFSheet sheet = workbook.getSheetAt(0);

            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator<Cell> cells = row.cellIterator();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();

                    org.apache.poi.ss.usermodel.CellType type = cell.getCellTypeEnum();
                    if (type == CellType.STRING) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = STRING; Value = "
                                + cell.getRichStringCellValue().toString());
                    } else if (type == CellType.NUMERIC) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = NUMERIC; Value ="
                                + cell.getNumericCellValue());
                    } else if (type == CellType.BOOLEAN) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = BOOLEAN; Value ="
                                + cell.getBooleanCellValue());
                    } else if (type == CellType.BLANK) {
                        System.out.println("[" + cell.getRowIndex() + ", "
                                + cell.getColumnIndex() + "] = BLANK CELL");
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试导入org.apache.poi.ss.usermodel.CellType

org.apache.poi.ss.usermodel.CellType type = cell.getCellTypeEnum();正在运行,但您不能在下一行以相同的方式致电CellType

答案 1 :(得分:0)

如果您使用的是最新版本 poi-4.0.1 ,则将所有if条件更改为:

 if (type == STRING) {
          // ..............
          // ..............
   } else if (type == NUMERIC) {
        // ..............
        // ..............
   } else if (type == BOOLEAN) {
        // ..............
        // ..............
  } else if (type == BLANK) {
       // ..............
      // ..............
                    }
                }