需要从 xlsx 文件访问数据

Need to access data from xlsx file

public class fileReader {

    public static void main (String[] args) throws IOException {
        String excelFilePath = "sample.xlsx";
        InputStream inputStream = new FileInputStream(new File(excelFilePath));

        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet firstSheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = firstSheet.iterator();

        while (iterator.hasNext()) {
            Row nextRow = iterator.next();
            Iterator<Cell> cellIterator = nextRow.cellIterator();
        ...

在 XSSFWorkbook 行中显示错误

Exception in thread "main" org.apache.poi.ooxml.POIXMLException: org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:66)
    at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:657)
    at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:282)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:303)
    at fileReader.main(fileReader.java:20)

您没有在阅读您的文件。您正在创建一个新工作簿。所以我怀疑你得到了一个例外,因为你要求 Sheet 那里有 none.

使用 XSSFWorkbook(File)

将您的文件提供给实例
String excelFilePath = "sample.xlsx";
File f = new File(excelFilePath);
XSSFWorkbook workbook = new XSSFWorkbook(f);

XSSFWorkbook(InputStream)

String excelFilePath = "sample.xlsx";
File f = new File(excelFilePath);
InputStream inputStream = new FileInputStream(f);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

If File is given to the workbook instance then there occurs an error showing File cannot be resolved to a type

只需要导入java.io.File