Formato Date não funciona - API POI (resolvido)

1 resposta
rafael_lagatta

Bom dia Pessoal,

Andei pesquisando, mas não achei algo relacionado. Quando exporto minha JTable para o excel, ele exporta em formato text, porém preciso de que uma coluna seja no formato date para aplicaro filtro.
Apliquei algumas regras, porém não obtive sucesso.
O código segue abaixo…
Só entra no formato date, quando abro o excel e do um F2 em cada célula, ai sim fica convertida para o formato desejado.

Alguma sugestão?

public void salvandoexcel(String saida) throws IOException {

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheetDADOS = wb.createSheet(Dados);

HSSFCellStyle style = wb.createCellStyle();

style.setDataFormat(HSSFDataFormat.getBuiltinFormat(m/d/yy));

FileOutputStream fileOut = new FileOutputStream(saida + .xls);

try {

for (int i = 0; i < jTable1.getRowCount(); ++i) {

sheetDADOS.createRow(i);

//for (int j = 0; j < jTable1.getColumnCount(); ++j) {

int j = 12;

try {

String s = jTable1.getValueAt(i, j).toString();

sheetDADOS.getRow(i).createCell((short) j);

sheetDADOS.getRow(i).getCell((short) j).setCellValue(new java.util.Date());

sheetDADOS.getRow(i).getCell((short) j).setCellValue(s.trim());

<a href="//sheetDADOS.getRow">//sheetDADOS.getRow</a>(i).getCell((short) j).setCellValue(s);

sheetDADOS.getRow(i).getCell((short) j).setCellStyle(style);

} catch (Exception exc) {

exc.printStackTrace();

break;

}

//}

}

wb.write(fileOut);

fileOut.flush();

fileOut.close();

JOptionPane.showMessageDialog(null, Salvo com sucesso!!);

} catch (Exception ex) {

fileOut.close();

ex.printStackTrace();

JOptionPane.showMessageDialog(null, Erro ao tentar salvar arquivo!!);

}

}

Vlw!!

1 Resposta

rafael_lagatta

Boa noite,

Depois de alguns testes e pesquisas, encontrei a solução, que por sinal estava na cara.
Quando crio o código abaixo, a variável “s” est´em formato string:

String s = jTable1.getValueAt(i, j).toString();

sheetDADOS.getRow(i).createCell((short) j);

sheetDADOS.getRow(i).getCell((short) j).setCellValue(new java.util.Date());

sheetDADOS.getRow(i).getCell((short) j).setCellValue(s.trim());

<a href="//sheetDADOS.getRow">//sheetDADOS.getRow</a>(i).getCell((short) j).setCellValue(s);

Logo precisamos convete-la para o formato Date!
Ficando assim:

String s = jTable1.getValueAt(i, j).toString();
                    sheetDADOS.getRow(i).createCell((short) j);
                    if (j != 11 && j != 12) {
                        sheetDADOS.getRow(i).getCell((short) j).setCellValue(s);
                    } else {
                        if (i == 0) {
                            sheetDADOS.getRow(i).getCell((short) j).setCellValue(s);
                        } else {
                            if (s == null || s.equals("")) {
                                break;
                            } else {
                                java.sql.Date date = null;
                                try {
                                    [b]DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                                    date = new java.sql.Date(((java.util.Date) formatter.parse(s)).getTime());[/b]
                                } catch (ParseException e) {
                                    throw e;
                                }
                                sheetDADOS.getRow(i).getCell((short) j).setCellStyle(style);
                                sheetDADOS.getRow(i).getCell((short) j).setCellValue(date);
                            }
                        }
                    }

Embora não tive respostas, fica ai para quem tiver alguma dúvida semelhante.

Criado 7 de maio de 2008
Ultima resposta 7 de mai. de 2008
Respostas 1
Participantes 1