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();
}
}
}
While executing this code it is giving error Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59) at org.apache.poi.poifs.property.DirectoryProperty.(DirectoryProperty.java:52) at org.apache.poi.poifs.property.RootProperty.(RootProperty.java:31) at org.apache.poi.poifs.property.PropertyTable.(PropertyTable.java:58) at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:102) at org.apache.poi.poifs.filesystem.POIFSFileSystem.(POIFSFileSystem.java:274) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:221) at NewClass.main(NewClass.java:31) Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 9 more
Add Apache commons math to your dependencies.
For example in Maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
More options here: https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.6.1
More information about ClassNotFoundErrors can be found here: Why am I getting a NoClassDefFoundError in Java?