本文介绍了使用 apache poi 读取 xlsx 时主线程中出现 NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

[...]
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();
        }
    }
}

我添加的罐子是

  • 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

例外是

 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)

推荐答案

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

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

来自官方文档

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

*强调我的.

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

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

Java 1.6.0_18

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

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

这篇关于使用 apache poi 读取 xlsx 时主线程中出现 NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:42