java 使用 apache poi 读取 xlsx 时主线程中出现 NoSuchMethodError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26866398/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:47:07  来源:igfitidea点击:

NoSuchMethodError in main thread while reading xlsx using apache poi

javaexcelexceptionapache-poi

提问by rajasekhar

my code is

我的代码是

[...]
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 jars that i have added are

我添加的罐子是

  • poi-3.9.jar
  • poi-ooxml-3.9.jar
  • poi-ooxml-schemas-3.7.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar
  • poi-3.9.jar
  • poi-ooxml-3.9.jar
  • poi-ooxml-schemas-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)

采纳答案by mystarrocks

Recent versions of poi-ooxmlrequire you to use at least java 6. The method in question was only added since java 6. Consider upgrading your javaversion.

的最新版本poi-ooxml要求您至少使用java 6. 有问题的方法是从java 6. 考虑升级您的java版本。

From the official docs,

官方文档

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

OOXML jar 需要 stax 实现,但现在 Apache POI 需要Java 6,这是由 JRE 提供的,不需要额外的 stax jar。OOXML jar 过去需要 DOM4J,但现在代码已更改为使用 JAXP,并且不需要额外的 dom4j jar。

*Emphasis mine.

*强调我的。

Since the question was edited, I'm specifying the build this method was added:

由于问题已被编辑,因此我指定了添加此方法的构建:

java 1.6.0_18

java 1.6.0_18

So a minimum of this version must be used in OP's case.

因此,在 OP 的情况下,必须至少使用此版本。

回答by centic

This usually indicates that you somehow drag in outdated XML Parser interfaces, e.g. via some transient dependency to an old xml-api version or an outdated xerces jar, which is usually not needed any more at all as Java 6 provides all the XML parsing functionality out of the box.

这通常表明您以某种方式拖入过时的 XML Parser 接口,例如通过对旧的 xml-api 版本或过时的 xerces jar 的一些暂时依赖,这通常不再需要,因为 Java 6 提供了所有的 XML 解析功能的盒子。

See this discusionfor some more details.

有关更多详细信息,请参阅此讨论

回答by user4521284

I ran into exactly the same issue as you, and I found the following jar files have to be used to solve the program:

我遇到了和你完全一样的问题,我发现必须使用以下 jar 文件来解决程序:

  • poi-bin-3.8.zip
  • opencsv-2.3.jar
  • poi-ooxml-3.8.jar
  • poi-ooxml-schemas-3.8.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar
  • poi-bin-3.8.zip
  • opencsv-2.3.jar
  • poi-ooxml-3.8.jar
  • poi-ooxml-schemas-3.8.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar

Newer version will bring trouble.

新版本会带来麻烦。