NoClassDefFoundError:使用apache poi写入excel文件时出现UnsupportedFileFormatException

时间:2021-07-19 20:25:11

I am trying to write to an excel(.xlsx) file using Apache poi, I included the apache poi dependencies in my pom.xml file. But I am getting the following exception in execution.

我正在尝试使用Apache poi写入excel(.xlsx)文件,我在我的pom.xml文件中包含了apache poi依赖项。但是我在执行中遇到以下异常。

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at test.ExcelWriting.main(ExcelWriting.java:24)
    Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 13 more

The code and pom.xml is specified as follows.

代码和pom.xml指定如下。

I am getting the exception in the following line.

我在以下行中得到例外。

XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);

Code:

码:

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelWriting {

    public static void main(String[] args) throws IOException{
        File myFile = new File("/home/sabra/workspace/test/src/main/resources/test.xlsx");
        FileInputStream fis = new FileInputStream(myFile);

        // Finds the workbook instance for XLSX file
        XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);

        // Return first sheet from the XLSX workbook
        XSSFSheet mySheet = myWorkBook.getSheetAt(0);

        Map<String, Object[]> data = new HashMap<String, Object[]>();
        data.put("7", new Object[] {7d, "Sonya", "75K", "SALES", "Rupert"});
        data.put("8", new Object[] {8d, "Kris", "85K", "SALES", "Rupert"});
        data.put("9", new Object[] {9d, "Dave", "90K", "SALES", "Rupert"});

        // Set to Iterate and add rows into XLS file
        Set<String> newRows = data.keySet();

        // get the last row number to append new data          
        int rownum = mySheet.getLastRowNum();         

        for (String key : newRows) {

            // Creating a new Row in existing XLSX sheet
            Row row = mySheet.createRow(rownum++);
            Object [] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof String) {
                    cell.setCellValue((String) obj);
                } else if (obj instanceof Boolean) {
                    cell.setCellValue((Boolean) obj);
                } else if (obj instanceof Date) {
                    cell.setCellValue((Date) obj);
                } else if (obj instanceof Double) {
                    cell.setCellValue((Double) obj);
                }
            }
        }

        // open an OutputStream to save written data into XLSX file
        FileOutputStream os = new FileOutputStream(myFile);
        myWorkBook.write(os);
        myWorkBook.close();
    }
}

Pom:

双响炮:

<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>test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.11-beta2</version>
    </dependency>
  </dependencies>
</project>

2 个解决方案

#1


4  

I think You'r missing some classes "UnsupportedFileFormatException"

我想你错过了一些类“UnsupportedFileFormatException”

try to change the poi versions to the same and dont use the 3.11-beta2

尝试将poi版本更改为相同并且不要使用3.11-beta2

You can use both in version 3.12 http://mvnrepository.com/artifact/org.apache.poi

您可以在版本3.12 http://mvnrepository.com/artifact/org.apache.poi中使用它们

#2


1  

I tried your code using the Jar Libs and not maven (see the images of project) and it worked fine. So like Athi said, you are missing some libs or the type of your file is not a xlsx.

我使用Jar Libs尝试了你的代码,而不是maven(参见项目的图像),它运行正常。就像Athi所说的那样,你缺少一些libs,或者你的文件类型不是xlsx。

NoClassDefFoundError:使用apache poi写入excel文件时出现UnsupportedFileFormatException

NoClassDefFoundError:使用apache poi写入excel文件时出现UnsupportedFileFormatException

PS: I just changed this line of code from

PS:我刚刚更改了这行代码

 File myFile = new File("/home/sabra/workspace/test/src/main/resources/test.xlsx");

to

File myFile = new File("test.xlsx");

#1


4  

I think You'r missing some classes "UnsupportedFileFormatException"

我想你错过了一些类“UnsupportedFileFormatException”

try to change the poi versions to the same and dont use the 3.11-beta2

尝试将poi版本更改为相同并且不要使用3.11-beta2

You can use both in version 3.12 http://mvnrepository.com/artifact/org.apache.poi

您可以在版本3.12 http://mvnrepository.com/artifact/org.apache.poi中使用它们

#2


1  

I tried your code using the Jar Libs and not maven (see the images of project) and it worked fine. So like Athi said, you are missing some libs or the type of your file is not a xlsx.

我使用Jar Libs尝试了你的代码,而不是maven(参见项目的图像),它运行正常。就像Athi所说的那样,你缺少一些libs,或者你的文件类型不是xlsx。

NoClassDefFoundError:使用apache poi写入excel文件时出现UnsupportedFileFormatException

NoClassDefFoundError:使用apache poi写入excel文件时出现UnsupportedFileFormatException

PS: I just changed this line of code from

PS:我刚刚更改了这行代码

 File myFile = new File("/home/sabra/workspace/test/src/main/resources/test.xlsx");

to

File myFile = new File("test.xlsx");