需要从 xlsx 文件访问数据

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)


扬帆大鱼
浏览 123回答 1
1回答

墨色风雨

你没有阅读你的文件。您正在创建一个新工作簿。所以我怀疑你有一个例外,因为你要求一个Sheet没有的地方。将您的文件提供给实例使用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);如果将 File 提供给工作簿实例,则会出现错误,显示 File cannot be resolved to a type只需要导入java.io.File
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java