Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package CPUGalenia; import Entidades.Consulta; import Negocios.ConsultaBLO; import java.awt.Desktop; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; /** * * @author ernesto */ public class FrmExportarConsultas extends javax.swing.JInternalFrame { private DateFormat formatoFecha = new SimpleDateFormat("dd/MM/yyyy"); /** * Creates new form FrmExportarConsultas */ public FrmExportarConsultas() { initComponents(); this.rootPane.setDefaultButton(btnAceptar); txtFechaInicial.setText(formatoFecha.format(new Date())); txtFechaFinal.setText(formatoFecha.format(new Date())); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { btnCancelar = new javax.swing.JButton(); btnAceptar = new javax.swing.JButton(); txtFechaInicial = new javax.swing.JFormattedTextField(); txtFechaFinal = new javax.swing.JFormattedTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); setClosable(true); setTitle("Exportar consultas"); btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Cancelar.png"))); // NOI18N btnCancelar.setText("Cancelar"); btnCancelar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelarActionPerformed(evt); } }); btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Aceptar.png"))); // NOI18N btnAceptar.setText("Aceptar"); btnAceptar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAceptarActionPerformed(evt); } }); try { txtFechaInicial.setFormatterFactory( new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####"))); } catch (java.text.ParseException ex) { ex.printStackTrace(); } try { txtFechaFinal.setFormatterFactory( new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####"))); } catch (java.text.ParseException ex) { ex.printStackTrace(); } jLabel1.setText("Fecha inicial:"); jLabel2.setText("Fecha final:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(btnAceptar) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnCancelar)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtFechaFinal, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE) .addComponent(txtFechaInicial)))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtFechaInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtFechaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnAceptar).addComponent(btnCancelar)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed // TODO add your handling code here: this.dispose(); }//GEN-LAST:event_btnCancelarActionPerformed private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed // TODO add your handling code here: int numeroFila = 5; Date fechaInicial = null; Date fechaFinal = null; try { fechaInicial = formatoFecha.parse(txtFechaInicial.getText()); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Ingresa un fecha inicial correcta.", "CPUGalenia", 0); return; } try { fechaFinal = formatoFecha.parse(txtFechaFinal.getText()); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Ingresa un fecha final correcta.", "CPUGalenia", 0); return; } List<Consulta> listaConsultas = ConsultaBLO.obtenerTodosPorFecha(fechaInicial, fechaFinal); String rutaArchivo = System.getProperty("user.home") + "/CPUGalenia-Consultas.xls"; File archivoXLS = new File(rutaArchivo); if (archivoXLS.exists()) archivoXLS.delete(); try { archivoXLS.createNewFile(); } catch (IOException ex) { Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex); } Workbook libro = new HSSFWorkbook(); FileOutputStream archivo = null; try { archivo = new FileOutputStream(archivoXLS); } catch (FileNotFoundException ex) { Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex); } Sheet hoja = libro.createSheet("Consultas"); //TITULO Row titulo = hoja.createRow(0); Cell ctitulo = titulo.createCell(0); ctitulo.setCellValue("REPORTE DE CONSULTAS"); //FECHA Row fecha1 = hoja.createRow(2); Cell cFecha1 = fecha1.createCell(0); cFecha1.setCellValue("Fecha inicial:"); Cell cFecha11 = fecha1.createCell(1); cFecha11.setCellValue(formatoFecha.format(fechaInicial)); Row fecha2 = hoja.createRow(3); Cell cFecha2 = fecha2.createCell(0); cFecha2.setCellValue("Fecha final:"); Cell cFecha22 = fecha2.createCell(1); cFecha22.setCellValue(formatoFecha.format(fechaFinal)); // TITULOS Row encabezado = hoja.createRow(5); Cell tId = encabezado.createCell(0); tId.setCellValue("Id Consulta"); Cell tFecha = encabezado.createCell(1); tFecha.setCellValue("Fecha"); Cell tApellidoPaterno = encabezado.createCell(2); tApellidoPaterno.setCellValue("Apellido paterno"); Cell tApellidoMaterno = encabezado.createCell(3); tApellidoMaterno.setCellValue("Apellido materno"); Cell tNombres = encabezado.createCell(4); tNombres.setCellValue("Nombres"); Cell tFechaNacimiento = encabezado.createCell(5); tFechaNacimiento.setCellValue("Fecha de nacimiento"); Cell tSexo = encabezado.createCell(6); tSexo.setCellValue("Sexo"); Cell tDiagnostico = encabezado.createCell(7); tDiagnostico.setCellValue("Diagnostico"); for (Consulta consulta : listaConsultas) { numeroFila++; Row fila = hoja.createRow(numeroFila); Cell cId = fila.createCell(0); cId.setCellValue(consulta.getId()); Cell cFecha = fila.createCell(1); cFecha.setCellValue(formatoFecha.format(consulta.getFecha())); Cell cApellidoPaterno = fila.createCell(2); cApellidoPaterno.setCellValue(consulta.getPaciente().getApellidoPaterno()); Cell cApellidoMaterno = fila.createCell(3); cApellidoMaterno.setCellValue(consulta.getPaciente().getApellidoMaterno()); Cell cNombres = fila.createCell(4); cNombres.setCellValue(consulta.getPaciente().getNombres()); Cell cFechaNacimiento = fila.createCell(5); cFechaNacimiento.setCellValue(formatoFecha.format(consulta.getPaciente().getFechaNacimiento())); Cell cSexo = fila.createCell(6); cSexo.setCellValue(consulta.getPaciente().getSexo().toString()); Cell cDiagnostico = fila.createCell(7); cDiagnostico.setCellValue(consulta.getDiagnostico().getDescripcion()); } try { libro.write(archivo); } catch (IOException ex) { Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex); } try { archivo.close(); } catch (IOException ex) { Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex); } try { /*Y abrimos el archivo con la clase Desktop*/ Desktop.getDesktop().open(archivoXLS); } catch (IOException ex) { Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnAceptarActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAceptar; private javax.swing.JButton btnCancelar; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JFormattedTextField txtFechaFinal; private javax.swing.JFormattedTextField txtFechaInicial; // End of variables declaration//GEN-END:variables }