在java中读取Excel工作表时出现NoSuchFieldError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37618369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoSuchFieldError when reading Excel sheet in java
提问by William Tolliver
I've followed a simple guide to constructing a workbook using Apache POI XSSF. Following the same guide I was able to WRITE an Excel sheet, however when attempting to read from one, I'm receiving the error displayed after the code.
我遵循了使用 Apache POI XSSF 构建工作簿的简单指南。按照相同的指南,我能够编写 Excel 工作表,但是当尝试从其中读取时,我收到代码后显示的错误。
Code:
代码:
try {
FileInputStream file = new FileInputStream(new File("howtodoinjava_demo.xlsx"));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
// Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}
System.out.println("");
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
Error output:
错误输出:
Exception in thread "main" java.lang.NoSuchFieldError: RAW_XML_FILE_HEADER at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179) at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:228) at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:93) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:294) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:273) at com.wtolliver.spring.test.ReadExcel.readExcel(ReadExcel.java:18) at com.wtolliver.spring.test.App.main(App.java:17)
线程“main”中的异常 java.lang.NoSuchFieldError: RAW_XML_FILE_HEADER at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179) at org.apache.poi.openxml4j.opc.internal.ZipHelper .openZipStream(ZipHelper.java:228) 在 org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:93) 在 org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:294)在 org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) 在 org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:273) 在 com.wtolliver.spring.test.ReadExcel。 readExcel(ReadExcel.java:18) 在 com.wtolliver.spring.test.App.main(App.java:17)
采纳答案by William Tolliver
After looking around a bit. I browsed the documentation for APACHE POI, and saw that this was one of the constants (not that i know what that really means).
稍微环顾了一下之后。我浏览了APACHE POI的文档,发现这是常量之一(不是我知道这真正意味着什么)。
But eventually, I realized all the tutorials I used were pre-2014.
但最终,我意识到我使用的所有教程都是 2014 年之前的。
So I just changed my Maven POM to version 3.11 for both dependencies of apache-poi
, and poi-ooxml
.
所以我只是将我的 Maven POM 更改为 3.11 版,用于apache-poi
, 和poi-ooxml
.
Its working now.
它现在工作。
回答by Murali
I got the same error, try changing the XSSFWorkbook diclaration to HSSFWorkbook. It worked for me.
我遇到了同样的错误,尝试将 XSSFWorkbook 声明更改为 HSSFWorkbook。它对我有用。
回答by Kannan Msk
I got same error with different constant:
我用不同的常量得到了同样的错误:
Exception in thread "main"
java.lang.NoSuchFieldError
: RETURN_NULL_AND_BLANK
线程“main”中的异常
java.lang.NoSuchFieldError
:RETURN_NULL_AND_BLANK
Googled lot but no answer. I was using apache poi-ooxml
version 3.11. Later I changed to version 3.17. Then it was working fine.
谷歌搜索了很多,但没有答案。我使用的是 Apache poi-ooxml
3.11 版。后来我换到了3.17版本。然后它工作正常。
Hope this might helpful someone.
希望这可能对某人有所帮助。
回答by Tanay Sinha
For the following error "java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK"
对于以下错误“java.lang.NoSuchFieldError:RETURN_NULL_AND_BLANK”
Please perform following task: upgrade the APACHE POI 3.** to the latest version in my case i have done the following thing i was previously using the apache poi 3.09 later i have upgraded the lib to latest version i.e 3.12 and its work !!
请执行以下任务:在我的情况下将 APACHE POI 3.** 升级到最新版本我做了以下事情我以前使用的是 apache poi 3.09 后来我将 lib 升级到最新版本,即 3.12 及其工作!
Perform the following steps and your code will but make sure the error should be same.
执行以下步骤,您的代码将确保错误应该相同。