gpt4 book ai didi

java - 为什么我的 Apache POI 代码无法读取 excel 工作表?

转载 作者:行者123 更新时间:2023-11-29 04:29:48 25 4
gpt4 key购买 nike

为什么我的 Apache POI 代码无法读取 excel 工作表?

我的代码:

public class ReadExcelFile {
public static void main(String[] args) throws IOException {
Workbook book = new XSSFWorkbook(ReadExcelFile.class.getClassLoader().getResourceAsStream("C:\\Users\\Gi\\Desktop\\ExcelReader\\ExcelFramework\\src\\test\\java\\com\\ExcelFramework\\ExcelFramework\\data\\data.xlsx"));
Sheet sheet = book.getSheet("Sheet1");
Row row = sheet.getRow(1);
Cell cel = row.getCell(1);

System.err.println("Value: " + cel.getStringCellValue());
}

似乎出现了以下错误:

Exception in thread "main" java.lang.NullPointerException
at org.apache.poi.openxml4j.opc.internal.ZipHelper.prepareToCheckHeader(ZipHelper.java:211)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:228)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:97)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:342)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:285)
at com.ExcelFramework.ExcelFramework.ReadExcelFile.main(ReadExcelFile.java:11)

enter image description here

enter image description here

最佳答案

你可以尝试如下使用 FileInputStream :

    Workbook book = new XSSFWorkbook(new FileInputStream("C:\\Users\\Gi\\Desktop\\ExcelReader\\ExcelFramework\\src\\test\\java\\com\\ExcelFramework\\ExcelFramework\\data\\data.xlsx"));
Sheet sheet = book.getSheet("Sheet1");
Row row = sheet.getRow(1);
Cell cel = row.getCell(1);

System.err.println("Value: " + cel.getStringCellValue());

关于java - 为什么我的 Apache POI 代码无法读取 excel 工作表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44266861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com