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 Principal; import static Principal.prepagoGasolina.tablaArticulo; import conexion.ConnectionTableDB; import java.awt.Desktop; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFileChooser; import javax.swing.SwingConstants; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; 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 USUARIO */ public class reportesLubricantes extends javax.swing.JFrame { /** * Creates new form reportesLubricantes */ ConnectionTableDB model; ConnectionTableDB model2; String usu, contra; String inicial; String ffinal; String hini; String hfin; String usua; public reportesLubricantes(String user, String password, String vend, String codigoVend) { initComponents(); usu = user; contra = password; try { model2 = new ConnectionTableDB(usu, contra, "adv_facturacion", "SELECT nombre FROM adv_facturacion.usuarios where cargo='despachador' or cargo='secretaria';", false); usuarios.addItem(""); for (int i = 0; i < model2.getRowCount(); i++) { usuarios.addItem(model2.getValueAt(i, 0).toString()); } model2.desconectar(); } catch (SQLException ex) { Logger.getLogger(prepagoGasolina.class.getName()).log(Level.SEVERE, null, ex); } } /** * 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() { jPopupMenu2 = new javax.swing.JPopupMenu(); visualizar = new javax.swing.JMenuItem(); fechaI = new com.toedter.calendar.JDateChooser(); fechaF = new com.toedter.calendar.JDateChooser(); jScrollPane1 = new javax.swing.JScrollPane(); tablaReporte = new javax.swing.JTable(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); total = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); horaI = new javax.swing.JTextField(); horaF = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); usuarios = new javax.swing.JComboBox(); jLabel7 = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); radioFactura = new javax.swing.JRadioButton(); radioProducto = new javax.swing.JRadioButton(); jPopupMenu2.setName("visualizarpop"); // NOI18N visualizar.setText("visualizar"); visualizar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { visualizarActionPerformed(evt); } }); jPopupMenu2.add(visualizar); jPopupMenu2.getAccessibleContext().setAccessibleParent(tablaReporte); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("REPORTES LUBRICANTES"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { formWindowClosed(evt); } }); tablaReporte.setAutoCreateRowSorter(true); tablaReporte.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); tablaReporte.setComponentPopupMenu(jPopupMenu2); jScrollPane1.setViewportView(tablaReporte); jLabel1.setText("Fecha Inicial"); jLabel2.setText("Fecha Final"); jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/icoLupa.PNG"))); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); total.setEditable(false); total.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel3.setText("Hora Inicial"); jLabel4.setText("Hora Final"); jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel5.setText("Formato Hora HH:MM:SS"); jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel6.setText("Usuarios"); jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel7.setText("Fechas"); jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Iconos/icoExcel.png"))); // NOI18N jButton2.setToolTipText(""); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel8.setText("TOTAL"); jLabel9.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel9.setText("EXPORTAR"); radioFactura.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N radioFactura.setSelected(true); radioFactura.setText("Facturas Realizadas"); radioFactura.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { radioFacturaActionPerformed(evt); } }); radioProducto.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N radioProducto.setText("Productos Vendidos"); radioProducto.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { radioProductoActionPerformed(evt); } }); 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().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(91, 91, 91)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(radioProducto).addComponent(radioFactura)) .addGap(43, 43, 43) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup().addComponent(jLabel2) .addGap(55, 55, 55).addComponent(fechaF, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(fechaI, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(44, 44, 44) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent( usuarios, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(horaF, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(horaI, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(69, 69, 69).addComponent( jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(18, 18, 18) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 160, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10) .addComponent(jLabel9))) .addGap(249, 249, 249))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(5, 5, 5).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(23, 23, 23).addComponent(radioFactura) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(radioProducto)) .addGroup(layout.createSequentialGroup() .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel9)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(jLabel7)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(horaI, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(fechaI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)) .addGap(14, 14, 14) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(fechaF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addComponent(jLabel4)) .addComponent(horaF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jButton1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(usuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(13, 13, 13) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addContainerGap(22, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed DefaultTableCellRenderer tcr = new DefaultTableCellRenderer(); tcr.setHorizontalAlignment(SwingConstants.CENTER); if (radioFactura.isSelected()) { visualizar.setVisible(false); hini = horaI.getText(); hfin = horaF.getText(); String consulta1; String consulta2; try { String formato = fechaI.getDateFormatString(); Date dateI = fechaI.getDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); inicial = sdf.format(dateI); //JOptionPane.showMessageDialog(this, inicial); String ini[] = new String[2]; ini = inicial.split("-"); //String mesI=obtMes(ini[1]); inicial = ini[0] + "-" + ini[1] + "-" + ini[2]; //JOptionPane.showMessageDialog(this, inicial); String formato2 = fechaF.getDateFormatString(); Date dateF = fechaF.getDate(); sdf = new SimpleDateFormat("yyyy-MM-dd"); ffinal = sdf.format(dateF); String fi[] = new String[2]; fi = ffinal.split("-"); //String mesF=obtMes(fi[1]); ffinal = fi[0] + "-" + fi[1] + "-" + fi[2]; //JOptionPane.showMessageDialog(this, mesF); //JOptionPane.showMessageDialog(this, ffinal); //fnacim = String.valueOf(sdf.format(date)); } catch (Exception e) { Logger.getLogger(prepagoGasolina.class.getName()).log(Level.SEVERE, null, e); JOptionPane.showMessageDialog(null, "debe escoger las fechas ", "Error..!!", JOptionPane.ERROR_MESSAGE); } try { consulta1 = "select Estado_factura,factura.numero,factura.fecha,factura.metodo_pago,cliente.nombre as nombreClientes,cliente.cedula_ruc,usuarios.nombre as nombreUsuarios,factura.total\n" + "from factura,cliente,usuarios\n" + " where punto_emision_idpunto_emision=3 and fecha between '" + inicial + "' and '" + ffinal + "' and cliente_idcliente=idcliente\n" + " and usuarios_idusuarios=idusuarios"; if ((!hini.equalsIgnoreCase("")) & (!hfin.equalsIgnoreCase(""))) { consulta1 = consulta1 + " and hora between '" + hini + "' and '" + hfin + "'"; } usua = usuarios.getSelectedItem().toString(); if (!usua.equalsIgnoreCase("")) { consulta1 = consulta1 + " and usuarios.nombre ='" + usua + "'"; } //JOptionPane.showMessageDialog(this, usua); model = new ConnectionTableDB(usu, contra, "adv_facturacion", consulta1, false); tablaReporte.setModel(model); for (int i = 0; i < tablaReporte.getColumnCount(); i++) { tablaReporte.getColumnModel().getColumn(i).setCellRenderer(tcr); } // } catch (SQLException ex) { Logger.getLogger(prepagoGasolina.class.getName()).log(Level.SEVERE, null, ex); } try { consulta2 = "select sum(factura.total)\n" + "from factura,cliente,usuarios\n" + " where punto_emision_idpunto_emision=3 and fecha between '" + inicial + "' and '" + ffinal + "' and cliente_idcliente=idcliente\n" + " and usuarios_idusuarios=idusuarios \n" + " "; if ((!hini.equalsIgnoreCase("")) & (!hfin.equalsIgnoreCase(""))) { consulta2 = consulta2 + " and hora between '" + hini + "' and '" + hfin + "'"; } String usua = usuarios.getSelectedItem().toString(); if (!usua.equalsIgnoreCase("")) { consulta2 = consulta2 + " and usuarios.nombre ='" + usua + "'"; } System.out.println(consulta2); model2 = new ConnectionTableDB(usu, contra, "adv_facturacion", consulta2, false); if (model.getRowCount() == 0) { JOptionPane.showMessageDialog(this, "La consulta esta vacia"); total.setText("0.00"); } else { total.setText(model2.getValueAt(0, 0).toString()); } model2.desconectar(); } catch (SQLException ex) { Logger.getLogger(prepagoGasolina.class.getName()).log(Level.SEVERE, null, ex); } } if (radioProducto.isSelected()) { visualizar.setVisible(true); String inicial = ""; String ffinal = ""; String hini = horaI.getText(); String hfin = horaF.getText(); String consulta1; String consulta2; try { String formato = fechaI.getDateFormatString(); Date dateI = fechaI.getDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); inicial = sdf.format(dateI); //JOptionPane.showMessageDialog(this, inicial); String ini[] = new String[2]; ini = inicial.split("-"); //String mesI=obtMes(ini[1]); inicial = ini[0] + "-" + ini[1] + "-" + ini[2]; //JOptionPane.showMessageDialog(this, inicial); String formato2 = fechaF.getDateFormatString(); Date dateF = fechaF.getDate(); sdf = new SimpleDateFormat("yyyy-MM-dd"); ffinal = sdf.format(dateF); String fi[] = new String[2]; fi = ffinal.split("-"); //String mesF=obtMes(fi[1]); ffinal = fi[0] + "-" + fi[1] + "-" + fi[2]; } catch (Exception e) { JOptionPane.showMessageDialog(null, "debe escoger las fechas ", "Error..!!", JOptionPane.ERROR_MESSAGE); } try { consulta1 = "select codigo_barra as 'CODIGO',producto.nombre as 'DESCRIPCION',producto.punit as PRECIO_VENTA_PUBLICO,sum(factura_detalle.cantidad) as 'CANTIDAD PRODUCTOS VENDIDOS' \n" + "from factura,cliente,usuarios,factura_detalle,producto\n" + " where punto_emision_idpunto_emision=3 and fecha between '" + inicial + "' and '" + ffinal + "' and cliente_idcliente=idcliente\n" + " and usuarios_idusuarios=idusuarios and idfactura=factura_idfactura\n" + " and producto.idproducto=producto_idproducto \n"; if ((!hini.equalsIgnoreCase("")) & (!hfin.equalsIgnoreCase(""))) { consulta1 = consulta1 + " and hora between '" + hini + "' and '" + hfin + "'"; } String usua = usuarios.getSelectedItem().toString(); if (!usua.equalsIgnoreCase("")) { consulta1 = consulta1 + " and usuarios.nombre ='" + usua + "'"; } consulta1 = consulta1 + " group by producto_idproducto"; //JOptionPane.showMessageDialog(this, usua); model = new ConnectionTableDB(usu, contra, "adv_facturacion", consulta1, false); tablaReporte.setModel(model); for (int i = 0; i < tablaReporte.getColumnCount(); i++) { tablaReporte.getColumnModel().getColumn(i).setCellRenderer(tcr); } } catch (SQLException ex) { Logger.getLogger(prepagoGasolina.class.getName()).log(Level.SEVERE, null, ex); } double total1 = 0; for (int i = 0; i < tablaReporte.getRowCount(); i++) { total1 = total1 + Double.parseDouble(String.valueOf(tablaReporte.getValueAt(i, 3))); } total.setText(total1 + ""); } // TODO add your handling code here: }//GEN-LAST:event_jButton1ActionPerformed private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed if (model.connected) { model.desconectar(); } // TODO add your handling code here: }//GEN-LAST:event_formWindowClosed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed //variable para comparacion if (tablaReporte.getRowCount() == 0) {//compara si en el JTextArea hay texto sino muestrtra un mensaje en pantalla JOptionPane.showMessageDialog(null, "LA TABLA ESTA VACIA", "Oops! Error", JOptionPane.ERROR_MESSAGE); } else { JFileChooser fileChooser = new JFileChooser(); fileChooser .addChoosableFileFilter(new FileNameExtensionFilter("todos los archivos *.xls", "xls", "xls"));//filtro para ver solo archivos .edu int seleccion = fileChooser.showSaveDialog(null); try { if (seleccion == JFileChooser.APPROVE_OPTION) {//comprueba si ha presionado el boton de aceptar File JFC = fileChooser.getSelectedFile(); String PATH = JFC.getAbsolutePath(); //obtenemos el path del archivo a guardar File JFC2 = new File(PATH + ".xls"); if (JFC.exists() | JFC2.exists()) { JOptionPane.showMessageDialog(this, "el archivo ya existe guarde con otro nombre"); } else { Workbook libro = new HSSFWorkbook(); FileOutputStream archivo = new FileOutputStream(JFC); Sheet hoja = libro.createSheet("Reporte "); for (int f = 0; f < model.getRowCount() + 4; f++) { /*La clase Row nos permitir crear las filas*/ Row fila = hoja.createRow(f); /*Cada fila tendr 5 celdas de datos*/ for (int c = 0; c < model.getColumnCount(); c++) { hoja.autoSizeColumn(c); /*Creamos la celda a partir de la fila actual*/ Cell celda = fila.createCell(c); /*Si la fila es la nmero 0, estableceremos los encabezados*/ if (f == 0) { celda.setCellValue(model.getColumnName(c)); } else { /*Si no es la primera fila establecemos un valor*/ if (f == model.getRowCount() + 1) { celda.setCellValue(""); } else { if (f == model.getRowCount() + 2) { if (c == model.getColumnCount() - 2) { celda.setCellValue("TOTAL"); } if (c == model.getColumnCount() - 1) { celda.setCellValue(total.getText()); } } else { if (model.getValueAt(f - 1, c).equals(null)) { celda.setCellValue(""); } else { celda.setCellValue(model.getValueAt(f - 1, c).toString()); } } } } } } /*Escribimos en el libro*/ libro.write(archivo); /*Cerramos el flujo de datos*/ archivo.close(); /*Y abrimos el archivo con la clase Desktop*/ if (!(PATH.endsWith(".xls"))) { File temp = new File(PATH + ".xls"); boolean a = JFC.renameTo(temp);//renombramos el archivo System.out.println("NO TERMINA"); System.out.println("cambiado" + a); } System.out.println("cmd /c start " + JFC.getPath() + ".xls"); try { Runtime.getRuntime().exec("cmd /c start " + JFC.getPath() + ".xls"); } catch (IOException e) { JOptionPane.showMessageDialog(null, "No se pudo abrir el archivo!", "Oops! Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } JOptionPane.showMessageDialog(null, "Guardado exitoso!", "Guardado exitoso!", JOptionPane.INFORMATION_MESSAGE); } //comprobamos si a la hora de guardar obtuvo la extension y si no se la asignamos } } catch (Exception e) {//por alguna excepcion salta un mensaje de error JOptionPane.showMessageDialog(null, "Error al guardar el archivo!", "Oops! Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } // TODO add your handling code here: }//GEN-LAST:event_jButton2ActionPerformed private void radioProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioProductoActionPerformed if (radioProducto.isSelected()) { radioFactura.setSelected(false); } // TODO add your handling code here: }//GEN-LAST:event_radioProductoActionPerformed private void radioFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioFacturaActionPerformed if (radioFactura.isSelected()) { radioProducto.setSelected(false); } // TODO add your handling code here: }//GEN-LAST:event_radioFacturaActionPerformed private void visualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_visualizarActionPerformed int fila = tablaReporte.getSelectedRow(); String cod = tablaReporte.getValueAt(fila, 0).toString(); String formato = fechaI.getDateFormatString(); Date dateI = fechaI.getDate(); SimpleDateFormat sdf = new SimpleDateFormat(formato); inicial = sdf.format(dateI); //JOptionPane.showMessageDialog(this, inicial); String ini[] = new String[2]; ini = inicial.split("/"); //String mesI=obtMes(ini[1]); inicial = ini[2] + "-" + ini[1] + "-" + ini[0]; //JOptionPane.showMessageDialog(this, inicial); String formato2 = fechaF.getDateFormatString(); Date dateF = fechaF.getDate(); sdf = new SimpleDateFormat(formato2); ffinal = sdf.format(dateF); String fi[] = new String[2]; fi = ffinal.split("/"); //String mesF=obtMes(fi[1]); ffinal = fi[2] + "-" + fi[1] + "-" + fi[0]; hini = horaI.getText(); hfin = horaF.getText(); usua = usuarios.getSelectedItem().toString(); //try { // model = new ConnectionTableDB(usu, contra, "adv_facturacion","select codigo_barra from producto where nombre ="+Tabla.getValueAt(fila, 1).toString(), false); //String codBar=model.getValueAt(0, 0).toString(); System.out.println("usu,contra,inicial,ffinal,hini,hfin,usua,cod"); System.out.println(usu + " " + contra + " " + inicial + " " + ffinal + " " + hini + " " + hfin + " " + usua + " " + cod); productosPorFactura ventana = new productosPorFactura(usu, contra, inicial, ffinal, hini, hfin, usua, cod); ventana.setVisible(true); /* } catch (SQLException ex) { Logger.getLogger(reportesLubricantes.class.getName()).log(Level.SEVERE, null, ex); }*/ //model.desconectar(); // TODO add your handling code here: }//GEN-LAST:event_visualizarActionPerformed public String obtMes(String mes) { String res = "0"; switch (mes) { case "ene": res = "01"; break; case "feb": res = "02"; break; case "mar": res = "03"; break; case "abr": res = "04"; break; case "may": res = "05"; break; case "jun": res = "06"; break; case "jul": res = "07"; break; case "ago": res = "08"; break; case "sep": res = "09"; break; case "oct": res = "10"; break; case "nov": res = "11"; break; case "dic": res = "11"; break; } return res; } /** * @param args the command line arguments */ // Variables declaration - do not modify//GEN-BEGIN:variables private com.toedter.calendar.JDateChooser fechaF; private com.toedter.calendar.JDateChooser fechaI; private javax.swing.JTextField horaF; private javax.swing.JTextField horaI; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPopupMenu jPopupMenu2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JRadioButton radioFactura; private javax.swing.JRadioButton radioProducto; private javax.swing.JTable tablaReporte; private javax.swing.JTextField total; private javax.swing.JComboBox usuarios; public javax.swing.JMenuItem visualizar; // End of variables declaration//GEN-END:variables }