NoSuchMethodError in main stream when reading xlsx using apache poi

my code

[...] import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFRow; public class ExcelRead { public static void main( String [] args ) { try { File excel = new File("Book1.xlsx"); FileInputStream fis = new FileInputStream(excel); XSSFWorkbook book = new XSSFWorkbook(fis); XSSFSheet sheet = book.getSheetAt(0); Iterator rows = sheet.rowIterator(); while( rows.hasNext() ) { HSSFRow row = (HSSFRow) rows.next(); System.out.println("\n"); Iterator cells = row.cellIterator(); while( cells.hasNext() ) { HSSFCell cell = (HSSFCell) cells.next(); if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) System.out.print( cell.getNumericCellValue()+" " ); else if(HSSFCell.CELL_TYPE_STRING==cell.getCellType()) System.out.print( cell.getStringCellValue()+" " ); else if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) System.out.print( cell.getBooleanCellValue()+" " ); else if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType()) System.out.print( "BLANK " ); else System.out.print("Unknown cell type"); } } } catch ( IOException ex ) { ex.printStackTrace(); } } } 

the banks that I added

  • poi-3.9.jar
  • poi-ooxml-3.9.jar
  • poi-ooxml-schema-3.7.jar
  • XMLBeans-2.3.0.jar
  • dom4j-1.6.1.jar

the exception is

  Exception in thread "main" java.lang.NoSuchMethodError: javax.xml.stream.XMLEventFactory.newFactory()Ljavax/xml/stream/XMLEventFactory; at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:45) at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:161) at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141) at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37) at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:87) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:254) at com.symplocus.ExcelRead.main(ExcelRead.java:26) 
+6
source share
3 answers

The latest versions of poi-ooxml require at least java 6 . This method was added only with java 6 . Try updating the java version.

In official docs

OOXML banks require a stax implementation, but now that Apache POI requires Java 6 provided by the JRE and no additional stax banners are required. OOXML jars used DOM4J, but now the code has been changed to use JAXP, and no additional dom4j jars are required.

* Focus on mine.

Since the question has been edited, I indicate that this method has been added :

java 1.6.0_18

Therefore, the minimum version of this version should be used in the case of OP.

+5
source

This usually means that you are somehow dragging out the legacy XML Parser interfaces, for example. through some transitional dependence on the old version of xml-api or the outdated xerces jar, which is usually no longer needed at all, since Java 6 provides all the XML parsing functions out of the box.

See this discussion for more details.

+1
source

I encountered the same problem as you, and I found the following jar files that must be used to solve the program:

  • poi-bin-3.8.zip
  • opencsv-2.3.jar
  • poi-ooxml-3.8.jar
  • poi-ooxml-schema-3.8.jar
  • XMLBeans-2.3.0.jar
  • dom4j-1.6.1.jar

A newer version will bring problems.

0
source

Source: https://habr.com/ru/post/1206635/


All Articles