Fala Galera!
Pessoal, estou com um probleminha aqui para exportar os dados da minha Jtable para Excel, está funcionando perfeitamente, porém tem um campo Date chamado Finalização, este eu sempre deixo vazio e só preencho quando finalizo o processo, o problema está aqui, quando este campo Date está vazio meu método de exportar não exporta nada, somente uma planilha vazia, segue os códigos para voces darem uma olhada:
Método para exportar Jtable:
//Metodo exportar tabela para Excel
public void toExcel(JTable table, File file) throws IOException {
TableModel model = table.getModel();
FileWriter excel = new FileWriter(file);
for (int i = 0; i < model.getColumnCount(); i++) {
excel.write(model.getColumnName(i) + "\t");
}
excel.write("\n");
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
excel.write(model.getValueAt(i, j).toString() + "\t");
}
excel.write("\n");
}
excel.close();
System.out.println("write out to: " + file);
}
Evento Click do botão para exportar:
//Botão Exportar Tabela para Excel
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
int option = fc.showSaveDialog(RdiGrid);
if (option == JFileChooser.APPROVE_OPTION) {
String filename = fc.getSelectedFile().getName();
String path = fc.getSelectedFile().getParentFile().getPath();
int len = filename.length();
String ext = "";
String file = "";
if (len > 4) {
ext = filename.substring(len - 4, len);
}
if (ext.equals(".xls")) {
file = path + "\\" + filename;
} else {
file = path + "\\" + filename + ".xls";
}
try {
toExcel(RdiGrid, new File(file));
} catch (IOException ex) {
Logger.getLogger(formRdi.class.getName()).log(Level.SEVERE, null, ex);
}
}
}