InicioApuntes Y MonografiasImporta y Exporta Jtable a Excel

Importa y Exporta Jtable a Excel

/*1RO CREAMOS LA CLASE*/ package excelimporter; import java.io.*; import javax.swing.*; import jxl.write.*; import jxl.*; public class ExcelTableExporter { private File file; private JTable table; private String nombreTab; public ExcelTableExporter(JTable table,File file,String nombreTab){ this.file=file; this.table=table; this.nombreTab=nombreTab; } public boolean export(){ try { //Nuestro flujo de salida para apuntar a donde vamos a escribir DataOutputStream out=new DataOutputStream(new FileOutputStream(file)); //Representa nuestro archivo en excel y necesita un OutputStream para saber donde va locoar los datos WritableWorkbook w = Workbook.createWorkbook(out); WritableSheet s = w.createSheet(nombreTab, 0); for(int i=0;i< table.getColumnCount();i++){ for(int j=0;j private void initComponents() { jFileChooser1 = new javax.swing.JFileChooser(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton1 = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jScrollPane1.setViewportView(jTable1); jButton1.setText("jButton1"; jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jMenu1.setText("Archivo"; jMenuItem1.setText("Importar"; jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuBar1.add(jMenu1); setJMenuBar(jMenuBar1); 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) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButton1) .addContainerGap(188, Short.MAX_VALUE)) ); pack(); }// private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { int answer = this.jFileChooser1.showDialog(null, "Importar"; if (answer == JFileChooser.APPROVE_OPTION) { File archivo = jFileChooser1.getSelectedFile(); if (archivo != null) { createTableModel(archivo); } } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { ExcelTableExporter excelExporter = new ExcelTableExporter (jTable1, new File("exportar.xls","Prueba"; if (excelExporter.export()) { JOptionPane.showMessageDialog(null, "Exportado con exito!"; } } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JFileChooser jFileChooser1; private javax.swing.JMenu jMenu1; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration public static void main(String[] args) { new DialogoImport(null, false).setVisible(true); } }
Datos archivados del Taringa! original
0puntos
0visitas
0comentarios
Actividad nueva en Posteamelo
0puntos
2visitas
0comentarios
Dar puntos:

Dejá tu comentario

0/2000

Autor del Post

P
PhoenixCamus🇦🇷
Usuario
Puntos0
Posts1
Ver perfil →
PosteameloArchivo Histórico de Taringa! (2004-2017). Preservando la inteligencia colectiva de la internet hispanohablante.

CONTACTO

18 de Septiembre 455, Casilla 52

Chillán, Región de Ñuble, Chile

Solo correo postal

© 2026 Posteamelo.com. No afiliado con Taringa! ni sus sucesores.

Contenido preservado con fines históricos y culturales.