I'm importing a file into Excel and playing inside a jTable, so that's fine. What happens is that user has inserted spreadsheet with empty fields. I would like to leave the entire sheet in red and request that it be changed.
This is the code I use to import the spreadsheet into jTable
DefaultTableModel modeloTable = new DefaultTableModel();
// ModeloTabela modeloTable = new ModeloTabela(null, col)
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();
if (qtdColunas == -1) {
break;
}
Object[] listaColuna = new Object[qtdColunas];
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;
}
modeloTable.addColumn(celula.getStringCellValue());
} else {
if (celula != null) {
switch (celula.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
if (tipoImportacao == 0) {
if (celula.getColumnIndex() == 3) {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
listaColuna[indiceColuna] = df.format(celula.getDateCellValue());
break;
} else if (celula.getColumnIndex() == 4) {
XSSFCellStyle celStyle = book.createCellStyle();
XSSFDataFormat dataFor = book.createDataFormat();
celStyle.setDataFormat(dataFor.getFormat("#,##0.00"));
celula.setCellStyle(celStyle);
listaColuna[indiceColuna] = celula.getNumericCellValue();
break;
}
listaColuna[indiceColuna] = (int) celula.getNumericCellValue();
// System.out.println(celula.getNumericCellValue());
break;
} else if (tipoImportacao == 1) {
listaColuna[indiceColuna] = (int) celula.getNumericCellValue();
// System.out.println(celula.getNumericCellValue());
break;
}
listaColuna[indiceColuna] = (int) celula.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
listaColuna[indiceColuna] = celula.getStringCellValue();
// System.out.println(celula.getStringCellValue());
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:
//caso seja uma linha em branco não sera apresentada na tabela
if (indiceColuna == 0) {
flagVazio = 1;
}
break;
case Cell.CELL_TYPE_ERROR:
break;
default:
break;
}
}
}
}//fim do iterator coluna
if (flagVazio == 1) {
indiceLinha--;
flagVazio = 0;
} else if (indiceLinha != 0) {
modeloTable.addRow(listaColuna);
}
}//fim do iterator linha
As you can see in this code I'm using DefaultTableModel because I could not filter the case for date, value among other things.
The type of worksheet I receive is
As the table is editable, to prevent the user from returning in Excel, typing what is missing and then trying again I would like to leave the rows with error in red.