This is the code I use to export to excel:
@SuppressWarnings("resource")
protected void btnExportarExcelActionPerformed(ActionEvent arg0) {
JFileChooser seleccionar = new JFileChooser();
File archivo;
if (seleccionar.showDialog(null, "Exportar a Excel") == JFileChooser.APPROVE_OPTION){
archivo = seleccionar.getSelectedFile();
int cantFila = tbNivelArchivo.getRowCount();
int cantColumna = tbNivelArchivo.getColumnCount();
XSSFWorkbook wb;
wb = new XSSFWorkbook();
Sheet hoja = ((org.apache.poi.ss.usermodel.Workbook) wb).createSheet(" ");
try {
for (int i = -1; i < cantFila; i++) {
Row fila = hoja.createRow(i + 1);
for (int j = 0; j < cantColumna; j++) {
Cell celda = fila.createCell(j);
if (i == -1) {
celda.setCellValue(String.valueOf(tbNivelArchivo.getColumnName(j)));
} else {
celda.setCellValue(String.valueOf(tbNivelArchivo.getValueAt(i, j)));
}
wb.write(new FileOutputStream(archivo + ".xlsx"));
}
}
JOptionPane.showMessageDialog(null, "Exportacion exitosa");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Vuelve a intentarlo");
}
}
}
If the excel is generated but it does not open automatically.