Results 1 to 2 of 2
  1. #1
    lazy is offline Member
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Exclamation JTable problems export to Microsoft Excel

    Hi guys! I have problems with JTable. When I open Excel file to JTable and save his to xls all good, but when I add dynamicly row to JTable and export JTable to Excel simply create an empty file without data. Please help me! Sorry for my bad English. Thanks

    Thats my code, I'm working in NetBeans

    Java Code:
    package javaapplication6;
    
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.print.PrinterException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.filechooser.FileNameExtensionFilter;
    import javax.swing.table.*;
    //Для работы с Excel
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    
    
    public class NewJFrame extends javax.swing.JFrame {
        
        // Open, save and search on JTable
       
        Vector headers  = new Vector();
        Vector data  = new Vector();
        DefaultTableModel model;
        
        public NewJFrame() {
            initComponents();
            model = (DefaultTableModel) jTable1.getModel();
            
            
            setExtendedState(MAXIMIZED_BOTH);
            
        }
    
        /**
         * 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">                          
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox();
            jLabel3 = new javax.swing.JLabel();
            jTextField2 = new javax.swing.JTextField();
            jLabel4 = new javax.swing.JLabel();
            jTextField3 = new javax.swing.JTextField();
            jLabel5 = new javax.swing.JLabel();
            jTextField4 = new javax.swing.JTextField();
            jLabel6 = new javax.swing.JLabel();
            jComboBox2 = new javax.swing.JComboBox();
            jLabel7 = new javax.swing.JLabel();
            jComboBox3 = new javax.swing.JComboBox();
            jLabel8 = new javax.swing.JLabel();
            jComboBox4 = new javax.swing.JComboBox();
            jLabel9 = new javax.swing.JLabel();
            jComboBox5 = new javax.swing.JComboBox();
            jTextField5 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jLabel10 = new javax.swing.JLabel();
            jComboBox6 = new javax.swing.JComboBox();
            jLabel11 = new javax.swing.JLabel();
            jComboBox7 = new javax.swing.JComboBox();
            jLabel12 = new javax.swing.JLabel();
            jComboBox8 = new javax.swing.JComboBox();
            jLabel13 = new javax.swing.JLabel();
            jComboBox9 = new javax.swing.JComboBox();
            jLabel14 = new javax.swing.JLabel();
            jComboBox10 = new javax.swing.JComboBox();
            jLabel15 = new javax.swing.JLabel();
            jComboBox11 = new javax.swing.JComboBox();
            jLabel16 = new javax.swing.JLabel();
            jComboBox12 = new javax.swing.JComboBox();
            jLabel17 = new javax.swing.JLabel();
            jComboBox13 = new javax.swing.JComboBox();
            jLabel18 = new javax.swing.JLabel();
            jComboBox14 = new javax.swing.JComboBox();
            jLabel19 = new javax.swing.JLabel();
            jComboBox15 = new javax.swing.JComboBox();
            jButton2 = new javax.swing.JButton();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu2 = new javax.swing.JMenu();
            jMenuItem1 = new javax.swing.JMenuItem();
            jMenuItem2 = new javax.swing.JMenuItem();
            jMenuItem3 = new javax.swing.JMenuItem();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTable1.setAutoCreateRowSorter(true);
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
    
                },
                new String [] {
                    "№", "П.І.Б", "Рік народження", "Адреса", "Професія", "Ким працює", "Вперше/Група", "Група", "Причина інвалідності", "Строк", "Основний діагноз", "Супутній діагноз", "Індивідуальна програма реабілітації", "Висновок/Зміна категорії", "Консультації", "Реабілітація", "Чоловіки/Жінки", "Місто/Село", "УБД/УВВ", "Первинно/Повторно"
                }
            ) {
                Class[] types = new Class [] {
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                };
                boolean[] canEdit = new boolean [] {
                    false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
                };
    
                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }
    
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            });
            jScrollPane1.setViewportView(jTable1);
    
            jLabel1.setText("П.І.Б");
    
            jLabel2.setText("Рік народження");
    
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " 2014", " 2013", " 2012", " 2011", " 2010", " 2009", " 2008", " 2007", " 2006", " 2005", " 2004", " 2003", " 2002", " 2001", " 2000", " 1999", " 1998", " 1997", " 1996", " 1995", " 1994", " 1993", " 1992", " 1991", " 1990", " 1989", " 1988", " 1987", " 1986", " 1985", " 1984", " 1983", " 1982", " 1981", " 1980", " 1979", " 1978", " 1977", " 1976", " 1975", " 1974", " 1973", " 1972", " 1971", " 1970", " 1969", " 1968", " 1967", " 1966", " 1965", " 1964", " 1963", " 1962", " 1961", " 1960", " 1959", " 1958", " 1957", " 1956", " 1955", " 1954", " 1953", " 1952", " 1951", " 1950", " 1949", " 1948", " 1947", " 1946", " 1945", " 1944", " 1943", " 1942", " 1941", " 1940", " 1939", " 1938", " 1937", " 1936", " 1935", " 1934", " 1933", " 1932", " 1931", " 1930", " 1929", " 1928", " 1927", " 1926", " 1925", " 1924", " 1923", " 1922", " 1921", " 1920", " 1919", " 1918", "1917", "1916", "1915", "1914", "1913", "1912", "1911", "1910" }));
    
            jLabel3.setText("Адреса");
    
            jLabel4.setText("Професія");
    
            jLabel5.setText("Ким працює");
    
            jLabel6.setText("Вперше/Група");
    
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Вперше", "1А", "1Б", "2", "3" }));
    
            jLabel7.setText("Група");
    
            jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1А", "1Б", "2", "3" }));
    
            jLabel8.setText("Причина інвалідності");
    
            jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Загальне", "З дитинства" }));
    
            jLabel9.setText("Строк");
    
            jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "БТ" }));
    
            jButton1.setText("Пошук");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jLabel10.setText("Основний діагноз");
    
            jComboBox6.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    
            jLabel11.setText("Супутній діагноз");
    
            jComboBox7.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    
            jLabel12.setText("Індивідуальна програма реабілітації");
    
            jComboBox8.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Заповнено", "Не заповено", " " }));
    
            jLabel13.setText("Висновок/Зміна атегорії");
    
            jComboBox9.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Було", "Не було", " " }));
    
            jLabel14.setText("Консультації");
    
            jComboBox10.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Були", "Не було", " " }));
    
            jLabel15.setText("Реабілітація");
    
            jComboBox11.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Повна", "Часткова" }));
    
            jLabel16.setText("Чоловіки/Жінки");
    
            jComboBox12.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Чоловік", "Жінка" }));
    
            jLabel17.setText("Місто/Село");
    
            jComboBox13.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Місто", "Село" }));
    
            jLabel18.setText("УБД/УВВ");
    
            jComboBox14.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "УБД", "УВВ" }));
    
            jLabel19.setText("Первинно/Повторно");
    
            jComboBox15.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Первинно", "Повторно", " " }));
    
            jButton2.setText("jButton2");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jMenu2.setText("Файл");
    
            jMenuItem1.setText("Відкрити файл...");
            jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem1ActionPerformed(evt);
                }
            });
            jMenu2.add(jMenuItem1);
    
            jMenuItem2.setText("Зберегти файл...");
            jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem2ActionPerformed(evt);
                }
            });
            jMenu2.add(jMenuItem2);
    
            jMenuItem3.setText("Друк");
            jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem3ActionPerformed(evt);
                }
            });
            jMenu2.add(jMenuItem3);
    
            jMenuBar1.add(jMenu2);
    
            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()
                    .addGroup(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, 769, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel1)
                                        .addComponent(jTextField1)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jTextField2)
                                        .addComponent(jLabel4)
                                        .addComponent(jTextField3)
                                        .addComponent(jLabel5)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))
                                    .addGap(28, 28, 28)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel6)
                                        .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel7)
                                        .addComponent(jComboBox3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jComboBox4, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel10)
                                        .addComponent(jComboBox6, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGap(32, 32, 32)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel15)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                    .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addComponent(jComboBox7, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addComponent(jLabel12)
                                                .addComponent(jComboBox8, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel13)
                                                .addComponent(jLabel14))
                                            .addGap(41, 41, 41)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jLabel19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jComboBox14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel18)
                                                .addComponent(jLabel17)
                                                .addComponent(jLabel16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jComboBox12, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jComboBox13, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jComboBox15, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(jComboBox11, javax.swing.GroupLayout.Alignment.LEADING, 0, 91, Short.MAX_VALUE)
                                                .addComponent(jComboBox10, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jComboBox9, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                            .addGap(77, 77, 77)
                                            .addComponent(jButton2)))
                                    .addGap(0, 0, Short.MAX_VALUE))))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(255, 255, 255)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jLabel6)
                        .addComponent(jLabel11)
                        .addComponent(jLabel16))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel7)
                        .addComponent(jLabel12)
                        .addComponent(jLabel17))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jLabel8)
                        .addComponent(jLabel13)
                        .addComponent(jLabel18))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jLabel9)
                        .addComponent(jLabel14)
                        .addComponent(jLabel19))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jLabel10)
                        .addComponent(jLabel15))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jComboBox11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2))
                    .addGap(32, 32, 32)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
        
       // Open Excel file
        private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
            
            JFileChooser fileChooser = new JFileChooser();
            // Фильтр для файлов
            FileNameExtensionFilter xlsFilter = new FileNameExtensionFilter("Microsoft Excel (*.xls)", "xls");
            fileChooser.addChoosableFileFilter(xlsFilter);
            int value = fileChooser.showOpenDialog(this);
            if(value == JFileChooser.APPROVE_OPTION){
                File file = fileChooser.getSelectedFile();
                try {
            Workbook workbook = Workbook.getWorkbook(file);
            Sheet sheet = workbook.getSheet(0);
            headers.clear();
            for (int i = 0; i < sheet.getColumns(); i++) {
            Cell cell1 = sheet.getCell(i, 0);
            headers.add(cell1.getContents());
            }
            data.clear();
            for (int j = 1; j < sheet.getRows(); j++) {
            Vector d = new Vector();
            for (int i = 0; i < sheet.getColumns(); i++) {
            Cell cell = sheet.getCell(i, j);
            d.add(cell.getContents());
            }
            d.add("\n");
            data.add(d);
            
            }
            }
                
            catch (Exception e) {
            e.printStackTrace();
            }
                try{
            JOptionPane.showMessageDialog(null, "Відкрито файл " + file.getName());
                }
                catch(Exception ex){
                    JOptionPane.showMessageDialog(fileChooser, ex);
                }
            DefaultTableModel model = new DefaultTableModel(data, headers);
            jTable1.setModel(model);
    
            }
        }                                          
        //Save file to Excel
        private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
            JFileChooser fileChooser = new JFileChooser();
            int value = fileChooser.showSaveDialog(this);
            if(value == JFileChooser.APPROVE_OPTION){
                File file = fileChooser.getSelectedFile();
                try {
                    toExcel(jTable1, file);
                } catch (Exception e) {
                }
            }
        }                                          
        //Print
        private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            try {
                // TODO add your handling code here:
                jTable1.print();
            } catch (PrinterException ex) {
                Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                          
        // Search
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
        TableModel model = new DefaultTableModel(data, headers);
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
        jTable1.setRowSorter(sorter);
        String text = jTextField5.getText();
        if(text.length() == 0){
            sorter.setRowFilter(null);
        }
        else{
            sorter.setRowFilter(RowFilter.regexFilter(text));
        }
        }                                        
        // Add row to JTable
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            Object[] obj = new Object[5];
            obj[1] = jTextField1.getText();
            obj[2] = jComboBox1.getSelectedItem();
            obj[3] = jTextField2.getText();
            obj[4] = jTextField3.getText();
            model.addRow(obj);
            
            
        }                                        
    
        
    public void toExcel(JTable table, File file){
        try{
            TableModel model = table.getModel();
            FileWriter excel = new FileWriter(file);
    
            for(int i = 0; i < model.getColumnCount(); i++){
                excel.write(model.getColumnName(i) + "\t");
            }
    
            excel.write("\n");
    
            for(int i=0; i< model.getRowCount(); i++) {
                for(int j=0; j < model.getColumnCount(); j++) {
                    excel.write(model.getValueAt(i,j).toString()+"\t");
                }
                excel.write("\n");
            }
    
            excel.close();
    
        }catch(IOException e){ System.out.println(e); }
    }
    
        /**
         * @param args the command line arguments
         */
        
        public static void main(String args[]) {
            /* Set the System look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                    
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JComboBox jComboBox10;
        private javax.swing.JComboBox jComboBox11;
        private javax.swing.JComboBox jComboBox12;
        private javax.swing.JComboBox jComboBox13;
        private javax.swing.JComboBox jComboBox14;
        private javax.swing.JComboBox jComboBox15;
        private javax.swing.JComboBox jComboBox2;
        private javax.swing.JComboBox jComboBox3;
        private javax.swing.JComboBox jComboBox4;
        private javax.swing.JComboBox jComboBox5;
        private javax.swing.JComboBox jComboBox6;
        private javax.swing.JComboBox jComboBox7;
        private javax.swing.JComboBox jComboBox8;
        private javax.swing.JComboBox jComboBox9;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel10;
        private javax.swing.JLabel jLabel11;
        private javax.swing.JLabel jLabel12;
        private javax.swing.JLabel jLabel13;
        private javax.swing.JLabel jLabel14;
        private javax.swing.JLabel jLabel15;
        private javax.swing.JLabel jLabel16;
        private javax.swing.JLabel jLabel17;
        private javax.swing.JLabel jLabel18;
        private javax.swing.JLabel jLabel19;
        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.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jMenuItem1;
        private javax.swing.JMenuItem jMenuItem2;
        private javax.swing.JMenuItem jMenuItem3;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        private javax.swing.JTextField jTextField5;
        // End of variables declaration                   
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: JTable problems export to Microsoft Excel

    This appears to be code generated by a GUI builder, which is notoriously hard to read. That's probably why you haven't received any answers yet.

    You'll have much better luck if you post an MCVE instead.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. export jTable to csv/excel
    By zenith666 in forum New To Java
    Replies: 1
    Last Post: 01-17-2012, 03:35 AM
  2. [SOLVED] Export to excel
    By nehaa in forum JDBC
    Replies: 10
    Last Post: 05-06-2009, 07:26 PM
  3. Export to excel
    By nehaa in forum AWT / Swing
    Replies: 1
    Last Post: 04-30-2009, 02:44 PM
  4. export to excel
    By diana glz in forum Advanced Java
    Replies: 0
    Last Post: 03-13-2008, 11:01 PM
  5. export to excel
    By diana glz in forum New To Java
    Replies: 0
    Last Post: 03-13-2008, 09:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •