NoClassDefFoundError: UnsupportedFileFormatException while working with excel sheet using java
NoClassDefFoundError: UnsupportedFileFormatException while working with excel sheet using java
我正在编写一个 Java 程序,该程序从 .xlsx 文件读取并以 .csv 格式提供输出。这是我的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
public class xlsxToCsv {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
File inputFile = new File("C:\inputFile.xlsx");
File outputFile = new File("C:\outputFile.csv");
xlsx(inputFile, outputFile);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println(elapsedTime);
}
private static void xlsx(File inputFile, File outputFile) {
//for storing data into CSV files
StringBuffer data = new StringBuffer();
try {
Writer w = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8");
// Get the workbook object for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
System.out.println(date.toString());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String d = sdf.format(date);
System.out.println(d);
data.append(d + ",");
}
else if(cell.getNumericCellValue() == (int)cell.getNumericCellValue())
data.append((int)cell.getNumericCellValue() + ",");
else if(cell.getNumericCellValue() == (long)cell.getNumericCellValue())
data.append((long)cell.getNumericCellValue() + ",");
else
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append((cell.getStringCellValue()) + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
//data.append('\n');
}
data.append("\r\n");
}
w.write(data.toString());
w.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}
但是,我收到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at xlsxToCsv.xlsxToCsv.xlsx(xlsxToCsv.java:47)
at xlsxToCsv.xlsxToCsv.main(xlsxToCsv.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 14 more
我包含了以下 jar:
- dom4j-1.6.jar
- poi-3.9.jar
- poi-ooxml-3.11.jar
- poi-ooxml-schemas-3.8-20120326.jar
- xmlbeans-2.3.0.jar
我已经检查了文件格式是 .xlsx 和目录,但我不明白问题是什么。
如何消除这个错误?
NoClassDefFoundError 当 JVM 在 运行 时无法找到库时出现。可能是您在文件路径中添加了库,但它在 运行 时间不可用。
Can I mix POI jars from different versions?
No. This is not supported.
All POI jars in use must come from the same version. A combination such as poi-3.11.jar and poi-ooxml-3.9.jar is not supported, and will fail to work in unpredictable ways.
您将自己列为使用来自不同版本的 poi-3.9.jar
和 poi-ooxml-3.11.jar
,将无法正常工作。
您需要确保所有 POI jar 都来自同一版本。我建议您获取 latest POI release from the download page(撰写本文时为 3.12),并使用
中的一组一致的罐子
我也试过同样的错误。这是因为 poi
罐子在 pom.xml
文件中重复。
我收到了这些警告,但我先忽略了这些警告。后来我尝试删除 pom.xml
中重复的 poi
罐子,这解决了问题。
这是我之前收到的警告:
[WARNING] Some problems were encountered while building the effective
model for MavenProj:Perfumania:jar:0.0.1-SNAPSHOT [WARNING]
'dependencies.dependency.(groupId:artifactId:type:classifier)' must be
unique: org.apache.poi:poi:jar -> duplicate declaration of version 3.9
@ line 36,
原因是 poi 的版本,poi-ooxml,poi-ooxml-schemas 应该相同,例如您 rpom.xml 文件中的版本应该是 3.6
我正在编写一个 Java 程序,该程序从 .xlsx 文件读取并以 .csv 格式提供输出。这是我的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
public class xlsxToCsv {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
File inputFile = new File("C:\inputFile.xlsx");
File outputFile = new File("C:\outputFile.csv");
xlsx(inputFile, outputFile);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println(elapsedTime);
}
private static void xlsx(File inputFile, File outputFile) {
//for storing data into CSV files
StringBuffer data = new StringBuffer();
try {
Writer w = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8");
// Get the workbook object for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
System.out.println(date.toString());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String d = sdf.format(date);
System.out.println(d);
data.append(d + ",");
}
else if(cell.getNumericCellValue() == (int)cell.getNumericCellValue())
data.append((int)cell.getNumericCellValue() + ",");
else if(cell.getNumericCellValue() == (long)cell.getNumericCellValue())
data.append((long)cell.getNumericCellValue() + ",");
else
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append((cell.getStringCellValue()) + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
//data.append('\n');
}
data.append("\r\n");
}
w.write(data.toString());
w.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}
但是,我收到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at xlsxToCsv.xlsxToCsv.xlsx(xlsxToCsv.java:47)
at xlsxToCsv.xlsxToCsv.main(xlsxToCsv.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 14 more
我包含了以下 jar:
- dom4j-1.6.jar
- poi-3.9.jar
- poi-ooxml-3.11.jar
- poi-ooxml-schemas-3.8-20120326.jar
- xmlbeans-2.3.0.jar
我已经检查了文件格式是 .xlsx 和目录,但我不明白问题是什么。
如何消除这个错误?
NoClassDefFoundError 当 JVM 在 运行 时无法找到库时出现。可能是您在文件路径中添加了库,但它在 运行 时间不可用。
Can I mix POI jars from different versions?
No. This is not supported.
All POI jars in use must come from the same version. A combination such as poi-3.11.jar and poi-ooxml-3.9.jar is not supported, and will fail to work in unpredictable ways.
您将自己列为使用来自不同版本的 poi-3.9.jar
和 poi-ooxml-3.11.jar
,将无法正常工作。
您需要确保所有 POI jar 都来自同一版本。我建议您获取 latest POI release from the download page(撰写本文时为 3.12),并使用
中的一组一致的罐子我也试过同样的错误。这是因为 poi
罐子在 pom.xml
文件中重复。
我收到了这些警告,但我先忽略了这些警告。后来我尝试删除 pom.xml
中重复的 poi
罐子,这解决了问题。
这是我之前收到的警告:
[WARNING] Some problems were encountered while building the effective model for MavenProj:Perfumania:jar:0.0.1-SNAPSHOT [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.apache.poi:poi:jar -> duplicate declaration of version 3.9 @ line 36,
原因是 poi 的版本,poi-ooxml,poi-ooxml-schemas 应该相同,例如您 rpom.xml 文件中的版本应该是 3.6