简体   繁体   English

STRING 无法解析或不是字段

[英]STRING cannot be resolved or is not a field

ERROR : Exception in thread "main" java.lang.Error: Unresolved compilation problems: STRING cannot be resolved or is not a field,NUMERIC cannot be resolved or is not a field,BOOLEAN cannot be resolved or is not a field,BLANK cannot be resolved or is not a field.错误:线程“main”java.lang.Error 中的异常:未解决的编译问题:STRING 无法解析或不是字段,NUMERIC 无法解析或不是字段,BOOLEAN 无法解析或不是字段,BLANK 不能被解决或不是一个字段。 I am using below code.我正在使用下面的代码。

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();
        }
    }
}

Try to import org.apache.poi.ss.usermodel.CellType .尝试导入org.apache.poi.ss.usermodel.CellType

This org.apache.poi.ss.usermodel.CellType type = cell.getCellTypeEnum();这个org.apache.poi.ss.usermodel.CellType type = cell.getCellTypeEnum(); is working but you don't call CellType the same way on next lines.正在工作,但您不会在下一行以相同的方式调用CellType

If you are using the latest version poi-4.0.1 Change all your if condition should simply be :如果您使用的是最新版本poi-4.0.1更改所有 if 条件应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM