public static void main(String [] args){
String excelFilePath = "D:\\JavaBooks.xls";
try {
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Object[][] bookData = {
{"The Passionate Programmer", "Chad Fowler", 16},
{"Software Craftmanship", "Pete McBreen", 26},
{"The Art of Agile Development", "James Shore", 32},
{"Continuous Delivery", "Jez Humble", 41},
};
int rowCount = sheet.getLastRowNum();
for (Object[] aBook : bookData) {
Row row = sheet.createRow(++rowCount);
int columnCount = 0;
Cell cell = row.createCell(columnCount);
cell.setCellValue(rowCount);
for (Object field : aBook) {
cell = row.createCell(++columnCount);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) {
cell.setCellValue((Integer) field);
}
}
}
inputStream.close();
FileOutputStream outputStream = new FileOutputStream("D:\\JavaBooks.xls");
workbook.write(outputStream);
workbook.close();
outputStream.close();
} catch (IOException | EncryptedDocumentException
ex) {
ex.printStackTrace();
}
}
}
执行此代码时出现错误线程“主”中的异常java.lang.NoClassDefFoundError:org / apache / commons / math3 / util / ArithmeticUtils在org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)在org.apache.poi.poifs.property.DirectoryProperty(DirectoryProperty.java:52)在org.apache.poi.poifs.property.RootProperty(RootProperty.java:31)在org.apache.poi.poifs.property.PropertyTable。(PropertyTable.java:58)在org.apache.poi.poifs.filesystem.POIFSFileSystem。(POIFSFileSystem.java:102)在org.apache.poi.poifs.filesystem.POIFSFileSystem。(POIFSFileSystem.java:274)在org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)在org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:221)在NewClass.main(NewClass.java:31)引起原因:java.lang.ClassNotFoundException:org.apache.commons.math3.util.ArithmeticUtils在java.net.URLClassLoader.findClass(URLClassLoader.java:381)在java.lang.ClassLoader.loadClass(ClassLoader.java:424)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:335)在java.lang.ClassLoader.loadClass(ClassLoader.java:357)...另外9个
将Apache Commons数学添加到您的依赖项。
例如在Maven中:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
此处有更多选项:https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.6.1
有关ClassNotFoundErrors的更多信息,可以在这里找到Why am I getting a NoClassDefFoundError in Java?