Foros del Web » Programación para mayores de 30 ;) » Java »

¿Excel a JTable?

Estas en el tema de ¿Excel a JTable? en el foro de Java en Foros del Web. Muy buenas a todos. Quiero hacer precisamente lo que dice el titulo, importar datos de un archivo excel 2010 a un JTable. Ya he hecho ...
  #1 (permalink)  
Antiguo 04/06/2013, 08:35
 
Fecha de Ingreso: abril-2013
Mensajes: 9
Antigüedad: 11 años
Puntos: 0
¿Excel a JTable?

Muy buenas a todos.
Quiero hacer precisamente lo que dice el titulo, importar datos de un archivo excel 2010 a un JTable. Ya he hecho la mayoría del código, y funciona perfectamente. Logro leer como capturar las filas y columnas de un archivo excel, pero de ahí, no puedo ni tampoco se me ocurre como llevar los datos a un JTable. Pongo el código a continuación, ya que además a alguien le podría ser de ayuda.

Código PHP:
public class InsertarSolicitudes extends javax.swing.JFrame {

    public 
InsertarSolicitudes() {
        
initComponents();
    }

    @
SuppressWarnings("unchecked")
    
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    
private void initComponents() {

        
jTextField1 = new javax.swing.JTextField();
        
jPanel1 = new javax.swing.JPanel();
        
jLabel1 = new javax.swing.JLabel();
        
jPanel2 = new javax.swing.JPanel();
        
jLabel2 = new javax.swing.JLabel();
        
btnbuscar = new javax.swing.JButton();
        
txtnombrearchivo = new javax.swing.JTextField();
        
jLabel3 = new javax.swing.JLabel();
        
txtruta = new javax.swing.JTextField();
        
jScrollPane1 = new javax.swing.JScrollPane();
        
jTextArea1 = new javax.swing.JTextArea();

        
jTextField1.setText("jTextField1");

        
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        
jLabel1.setText("Insertar Solicitudes por Excel");

        
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Seleccionar Archivo"));

        
jLabel2.setText("Nombre Archivo Excel:");

        
btnbuscar.setText("Buscar");
        
btnbuscar.addActionListener(new java.awt.event.ActionListener() {
            public 
void actionPerformed(java.awt.event.ActionEvent evt) {
                
btnbuscarActionPerformed(evt);
            }
        });

        
txtnombrearchivo.setEnabled(false);

        
jLabel3.setText("Ruta Archivo Excel:");

        
txtruta.setEnabled(false);

        
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        
jPanel2.setLayout(jPanel2Layout);
        
jPanel2Layout.setHorizontalGroup(
            
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .
addGroup(jPanel2Layout.createSequentialGroup()
                .
addContainerGap()
                .
addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .
addComponent(jLabel2)
                    .
addComponent(jLabel3))
                .
addGap(181818)
                .
addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADINGfalse)
                    .
addGroup(jPanel2Layout.createSequentialGroup()
                        .
addComponent(txtnombrearchivojavax.swing.GroupLayout.DEFAULT_SIZE118Short.MAX_VALUE)
                        .
addGap(222222)
                        .
addComponent(btnbuscar))
                    .
addComponent(txtruta))
                .
addContainerGap(98Short.MAX_VALUE))
        );
        
jPanel2Layout.setVerticalGroup(
            
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .
addGroup(jPanel2Layout.createSequentialGroup()
                .
addGap(242424)
                .
addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .
addComponent(jLabel2)
                    .
addComponent(btnbuscar)
                    .
addComponent(txtnombrearchivojavax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE))
                .
addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .
addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .
addComponent(jLabel3)
                    .
addComponent(txtrutajavax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE))
                .
addContainerGap(33Short.MAX_VALUE))
        );

        
jTextArea1.setColumns(20);
        
jTextArea1.setRows(5);
        
jScrollPane1.setViewportView(jTextArea1);

        
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        
jPanel1.setLayout(jPanel1Layout);
        
jPanel1Layout.setHorizontalGroup(
            
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .
addGroup(jPanel1Layout.createSequentialGroup()
                .
addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADINGfalse)
                    .
addGroup(jPanel1Layout.createSequentialGroup()
                        .
addGap(127127127)
                        .
addComponent(jLabel1))
                    .
addGroup(jPanel1Layout.createSequentialGroup()
                        .
addContainerGap()
                        .
addComponent(jPanel2javax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEShort.MAX_VALUE))
                    .
addGroup(jPanel1Layout.createSequentialGroup()
                        .
addContainerGap()
                        .
addComponent(jScrollPane1)))
                .
addContainerGap(32Short.MAX_VALUE))
        );
        
jPanel1Layout.setVerticalGroup(
            
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .
addGroup(jPanel1Layout.createSequentialGroup()
                .
addContainerGap()
                .
addComponent(jLabel1)
                .
addGap(181818)
                .
addComponent(jPanel2javax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE)
                .
addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .
addComponent(jScrollPane1javax.swing.GroupLayout.PREFERRED_SIZE172javax.swing.GroupLayout.PREFERRED_SIZE)
                .
addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZEShort.MAX_VALUE))
        );

        
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        
getContentPane().setLayout(layout);
        
layout.setHorizontalGroup(
            
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .
addGroup(layout.createSequentialGroup()
                .
addComponent(jPanel1javax.swing.GroupLayout.PREFERRED_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.PREFERRED_SIZE)
                .
addGap(00Short.MAX_VALUE))
        );
        
layout.setVerticalGroup(
            
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .
addGroup(layout.createSequentialGroup()
                .
addComponent(jPanel1javax.swing.GroupLayout.DEFAULT_SIZEjavax.swing.GroupLayout.DEFAULT_SIZEShort.MAX_VALUE)
                .
addContainerGap())
        );

        
pack();
    }
// </editor-fold>                        

    
private void btnbuscarActionPerformed(java.awt.event.ActionEvent evt) {                                          
        
// TODO add your handling code here:

        
JFileChooser fileChooser = new JFileChooser();
        
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents""pdf"));
        
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("MS Office Documents""docx""xlsx""pptx"));
        
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Images""jpg""png""gif""bmp"));
        
fileChooser.setAcceptAllFileFilterUsed(true);
        
int result fileChooser.showOpenDialog(this);
        if (
result == JFileChooser.APPROVE_OPTION
        {
            
File selectedFile fileChooser.getSelectedFile();
            
System.out.println("Selected file: " selectedFile.getAbsolutePath());
            
txtnombrearchivo.setText(selectedFile.getName());
            
txtruta.setText(selectedFile.getAbsolutePath());
            
        }
        
        List 
cellDataList = new ArrayList();
        try
        {
            
Workbook workBook WorkbookFactory.create(new FileInputStream(fileChooser.getSelectedFile()));
            
XSSFSheet hssfSheet = (XSSFSheetworkBook.getSheetAt(0);
            
Iterator rowIterator hssfSheet.rowIterator();    
            while (
rowIterator.hasNext())
        {
            
XSSFRow hssfRow = (XSSFRowrowIterator.next();
            
Iterator iterator hssfRow.cellIterator();
            List 
cellTempList = new ArrayList();
            while (
iterator.hasNext())
        {
            
XSSFCell hssfCell = (XSSFCelliterator.next();
            
cellTempList.add(hssfCell);
        }
            
cellDataList.add(cellTempList);
            }
            }catch (
Exception e)
            {
e.printStackTrace();}
    
            for (
int i 0cellDataList.size(); i++){
            List 
cellTempList = (List) cellDataList.get(i);
            
            for (
int j 0cellTempList.size(); j++)
                {
                    
XSSFCell hssfCell = (XSSFCellcellTempList.get(j);
                    
String stringCellValue hssfCell.toString();
                    
System.out.print(stringCellValue"\t\t");
                    }
                    
System.out.println();
                 
                } 
El código esta perfecto. Lee el excel y me lo imprime por consola con sus respectivas columnas y filas, pero no sé como llevar ese resultado a un JTable. ¿Alguien me podría ayudar?

Etiquetas: jtable, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:27.