尝试从java中的excel电子表格读取时出现NoClassDefFoundError

时间:2017-04-13 19:11:39

标签: java excel eclipse maven apache-poi

我已经仔细研究了很多关于这方面的问题,我似乎无法找到解决问题的方法。 我目前正在上一个关于java web编程的课程,我们在尝试从excel电子表格中读取时使用Maven来管理我们的依赖项。

这是我得到的错误:

`Exception in thread "main" java.lang.NoClassDefFoundError: org/openxmlformats/schemas/spreadsheetml/x2006/main/CTSheet
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:91)
    at edu.cvtc.web.util.WorkbookUtility.retrivePeopleFromWorkbook(WorkbookUtility.java:33)
    at edu.cvtc.web.JavaIOExample.main(JavaIOExample.java:23)
Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more` 

这是造成它的代码:

 public class WorkbookUtility {

    public static List<Person> retrivePeopleFromWorkbook(final File inputFile) throws InvalidFormatException, IOException {

        final List<Person> people = new ArrayList<Person>();

        // NOTES: create a workbook from the input file.
        final Workbook workbook = WorkbookFactory.create(inputFile);

        final Sheet sheet = workbook.getSheetAt(0);
        // NOTES: iterate over each row in the worksheet from the workbook.
        for (final Row row : sheet) {
            final Cell firstNameCell = row.getCell(0);
            final Cell lastNameCell = row.getCell(1);
            final Cell ageCell = row.getCell(2);
            final Cell favColorCell = row.getCell(3);

            // NOTES: create a new person object for each row in the worksheet.
            final Person person = new Person(firstNameCell.getStringCellValue().trim(), lastNameCell.getStringCellValue().trim(), (int) (ageCell.getNumericCellValue()), favColorCell.getStringCellValue().trim());

            // NOTES: add each new person to our list of people
            people.add(person);
        }
        return people;
    }
} 

另外两个相关的课程:
  人

    public class Person {

    private String  firstName;
    private String  lastName;
    private int     age;
    private String  favoriteColor;


    public Person(final String firstName, final String lastName, final int age, final String favoriteColor) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.favoriteColor = favoriteColor;
    }


    public String getFirstName() {
        return firstName;
    }


    public String getLastName() {
        return lastName;
    }


    public int getAge() {
        return age;
    }


    public String getFavoriteColor() {
        return favoriteColor;
    }


    @Override
    public String toString() {
        return "Person [firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", favoriteColor=" + favoriteColor + "]";
    }

}  

和JavaIOExample

package edu.cvtc.web;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import edu.cvtc.web.modal.Person;
import edu.cvtc.web.util.WorkbookUtility;


public class JavaIOExample {

    private static final String INPUT_FILE = "WebContent/assets/JavaWebProgramming.xlsx";


    public static void main(String[] args) {
        // NOTES: Use an input file to print a list of people to the console
        final File inputFile = new File(INPUT_FILE);
        try {
            final List<Person> people = WorkbookUtility.retrivePeopleFromWorkbook(inputFile);

            for (final Person person : people) {
                System.out.println(person);
            }

        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

这是我的pom.xml文件:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MavenExample</groupId>
  <artifactId>MavenExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <testSourceDirectory>src/main/test</testSourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0.1</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.13</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.7.2</version>
    </dependency>
    <dependency>
        <groupId>stax</groupId>
        <artifactId>stax-api</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.9</version>
    </dependency>
  </dependencies>
</project>

对不起,如果没有正确编写某些内容,或者我有太多依赖项或其他内容,我花了最后几个小时尝试调试这些错误,我找不到问题。如果您需要更多信息,请告诉我们!我需要这个为我的班级工作,所以你能给我的任何帮助都非常感谢。

0 个答案:

没有答案