Bom dia desenvolvedores, estou tentando ler uma tabela excel(.xls) com a API POI.
Consegui ler algumas celulas, mas quando a celula possui uma data ele me retorna um erro.
segue o codigo:
public class LendoXls {
public static void main(String[] args) throws IOException {
FileInputStream inputS = null;
try {
File caminhoXls = new File("D:\\dados.xls");
inputS = new FileInputStream(caminhoXls);
// cria uma planilha com todas as abas
HSSFWorkbook work = new HSSFWorkbook(inputS);
// recuperando o primeira planilha(aba)
HSSFSheet planilha = work.getSheetAt(0);
// retorna todas as linhas da planilha(0)
Iterator<Row> linhaIterator = planilha.rowIterator();
while (linhaIterator.hasNext()) {
Row conteudoLinha = linhaIterator.next();
Iterator<Cell> celulaIterator = conteudoLinha.cellIterator();
while (celulaIterator.hasNext()) {
Cell conteudoCelula = celulaIterator.next();
switch (conteudoCelula.getColumnIndex()) {
case 0:
System.out.println("Nome: " + conteudoCelula.getStringCellValue());
break;
case 1:
System.out.println("Sobrenome: " + conteudoCelula.getStringCellValue());
break;
// aqui dá erro
case 2:
System.out.println("Data nascimento: " + conteudoCelula.getDateCellValue());
break;
}
}
}
} catch (FileNotFoundException ex) {
System.out.println("erro" + ex);
} finally {
inputS.close();
}
}
}
erro:
Nome: NOME
Sobrenome: SOBRENOME
Exception in thread “main” java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:654)
at org.apache.poi.hssf.usermodel.HSSFCell.getNumericCellValue(HSSFCell.java:679)
at org.apache.poi.hssf.usermodel.HSSFCell.getDateCellValue(HSSFCell.java:700)
at lendoxls.LendoXls.main(LendoXls.java:71)
C:\Users\Andre\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 1 segundo)