Exception in thread "main"
java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:946)
at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:260)
at ExcelReaderPOIExample.main(ExcelReaderPOIExample.java:38)
Solution:
The cells in an Excel Spreadsheet can of various types so its better to either use an if-else or a switch-case to check what kind of value the cell holds before trying to fetch it,
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getCellType() == CellType.NUMERIC) {
System.out.print((int) cell.getNumericCellValue() + "\t");
} else if (cell.getCellType() == CellType.STRING) {
System.out.print(cell.getNumericCellValue() + "\t");
} else if(cell.getCellType() == CellType.FORMULA) {
System.out.print(cell.getCellFormula() + "\t");
}
}

Solution POI IllegalStateException
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Error: Can not find the tag library descriptor for - Java
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations - Android
- How to always show Bookmarks bar on Google Chrome Browser - Chrome
- Python Program To Calculate Simple Interest (SimpleInterest.py) - Python
- [Fix] MySQL Docker ERROR 1045 (28000): Access denied for user root@localhost (using password: YES/NO) - MySQL
- Base 64 Index, Character and Binary Table - Html
- Convert Java List to Json String using Jackson - Java
- Python copy file from a source to destination - Python