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");
}
}

Have Questions? Post them here!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- What does b prefix before a String mean in Python? - Python
- How to check installed Tomcat version - Tomcat
- Get Wifi Details : Android Programming - Android
- Spring Boot: NamedParameterJdbcTemplate batch insert example - Java
- How to Compare Strings in Bash - Bash
- Fix: Eclipse Connection time out: github.com - Eclipse
- How to get UTC (GMT) time using JavaScript - JavaScript
- How to stop MongoDB Server running on Ubuntu - Ubuntu