Estou lendo uma planilha a partir da biblioteca POI, onde na primeira coluna tenho números separados por uma “/” Barra.
O método que estou usando pra fazer a leitura é
public void importarPlanilha(File arquivo, JTable tabela) {
int flagVazio = 0;
DefaultTableModel modeloTable = new DefaultTableModel();
try {
//Recebe o arquivo da planilha
XSSFWorkbook book = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream(arquivo));
//Pega a primeira folha da planilha
Sheet folha = book.getSheetAt(0);
//percorre as linhas da planilha
Iterator linhaItetator = folha.rowIterator();
//prepara o indice das linha para percorrer as celulas
int indiceLinha = -1;
tabela.setModel(modeloTable);
while (linhaItetator.hasNext()) {
indiceLinha++;
Row linha = (Row) linhaItetator.next();
Iterator colunaIterator = linha.cellIterator();
int qtdColunas = linha.getLastCellNum();
//verificar tratamento de criação de coluna
Object[] listaColuna = new Object[qtdColunas];
if (qtdColunas == -1) {
break;
}
int indiceColuna = -1;
while (colunaIterator.hasNext()) {
indiceColuna++;
Cell celula = (Cell) colunaIterator.next();
if (indiceLinha == 0) {
//ser for sem descrição a coluna, não vai ser criada
if (celula.getStringCellValue().equals("")) {
break;
}
//enquanto estiver na linha zero APENAS VAI CRIAR AS CULUNAS DA TABELA
modeloTable.addColumn(celula.getStringCellValue());
} else {
if (celula != null) {
switch (celula.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
if (celula.getColumnIndex() == 4) {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date teste = celula.getDateCellValue();
listaColuna[indiceColuna] = df.format(teste);
break;
} else if (celula.getColumnIndex() == 5) {
XSSFCellStyle celStyle = book.createCellStyle();
XSSFDataFormat dataFor = book.createDataFormat();
celStyle.setDataFormat(dataFor.getFormat("#,##0.00"));
celula.setCellStyle(celStyle);
// listaColuna[indiceColuna] = celula.getNumericCellValue();
break;
}
case Cell.CELL_TYPE_STRING:
listaColuna[indiceColuna] = celula.getStringCellValue();
System.out.println(celula.getStringCellValue()+"Indice Coluna>>"+indiceColuna);
break;
case Cell.CELL_TYPE_BOOLEAN:
listaColuna[indiceColuna] = celula.getBooleanCellValue();
System.out.println(celula.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
listaColuna[indiceColuna] = celula.getCellFormula();
System.out.println(celula.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
break;
case Cell.CELL_TYPE_ERROR:
break;
default:
break;
}
}
}
}//fim do iterator coluna
if (indiceLinha != 0) {
modeloTable.addRow(listaColuna);
}
}//fim do iterator linha
} catch (FileNotFoundException ex) {
Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidFormatException ex) {
Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
} catch (EncryptedDocumentException ex) {
Logger.getLogger(ControleImportPlanilha.class.getName()).log(Level.SEVERE, null, ex);
}
}
Como faço pra resolver este problema?