简体   繁体   中英

how to read xls file into jtable

Im having trouble with importing XLS data to jtable.

My program reads only the last row from XLS.

Here is my code:

JButton btnImportExcelFiles = new JButton("EXCEL FILES");

btnImportExcelFiles.setIcon(new ImageIcon(racunariAplikacija.class.getResource("/image/Excel-icon.png")));

btnImportExcelFiles.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent arg0)
    {
        FileFilter filter = new FileNameExtensionFilter("Excel Files", "xls");

        // here is my file chooser
        JFileChooser jf = new JFileChooser();
        jf.addChoosableFileFilter(filter);
        int rezultat = jf.showOpenDialog(null);

        if(rezultat == JFileChooser.APPROVE_OPTION)
        {
            String excelPath = jf.getSelectedFile().getAbsolutePath();
            ArrayList<Stavka>lista  = new ArrayList<>();
            Stavka stavka = new Stavka();
            File f = new File(excelPath);
            Workbook wb = null;
            try {
                wb = Workbook.getWorkbook(f);
            }
            catch (BiffException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            // this is where i call for nested forloop
            Sheet s = wb.getSheet(0);
            int row = s.getRows();
            int col = s.getColumns();
            System.out.println("redovi" + row + "kolone" + col);

            for (int i = 0; i < 18; i++) {
                for (int j = 0; j < col; j++) {
                    Cell c = s.getCell(j,i);
                    if(j==0) {stavka.setStavkaID(Integer.parseInt(c.getContents().toString()));}
                    else if(j==1) {}
                    else if(j==2) {stavka.setSifraKomponente(c.getContents().toString());}
                    else if(j==3) {stavka.setOpis(c.getContents().toString());}
                    else if(j==4) {stavka.setVrednost(c.getContents().toString());}
                    else if(j==5) {stavka.setKuciste(c.getContents().toString());}
                    else if(j==6) {stavka.setSektor(c.getContents().toString());}
                    else if(j==7) {stavka.setRack(c.getContents().toString());}
                    else if(j==8) {stavka.setProizvodjac(c.getContents().toString());}
                    else if(j==9) {stavka.setKolicina(Integer.parseInt(c.getContents().toString()));}
                    //System.out.println(c.getContents());

                }   

                // this is my tableModel
                lista.add(stavka);
                TabelaStavka stavka1 = new TabelaStavka(lista);
                tblAzuriranjeMagacina.setModel(stavka1);
            }
        }
    }
}

How can this be fixed?

I believe the error exists because you must create a new Stavka object for each line in your XLS, and then add it to 'lista'. Finally, I believe you also want to set the model ( TabelaStavka ) outside these for loops.

I don't have a full Java environment installed here, so forgive me if there is any syntax error below. The most important thing is that you understand what are the fixes you have to do in your code.

Also, never underestimate code formatting, you should consider using a better IDE, which helps out better formatting your source code.

JButton btnImportExcelFiles = new JButton("EXCEL FILES");

btnImportExcelFiles.setIcon(new ImageIcon(racunariAplikacija.class.getResource("/image/Excel-icon.png")));

btnImportExcelFiles.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent arg0)
    {
        FileFilter filter = new FileNameExtensionFilter("Excel Files", "xls");

        // here is my file chooser
        JFileChooser jf = new JFileChooser();
        jf.addChoosableFileFilter(filter);
        int rezultat = jf.showOpenDialog(null);

        if(rezultat == JFileChooser.APPROVE_OPTION)
        {
            String excelPath = jf.getSelectedFile().getAbsolutePath();
            ArrayList<Stavka>lista  = new ArrayList<>();
            File f = new File(excelPath);
            Workbook wb = null;
            try
            {
                wb = Workbook.getWorkbook(f);
            }
            catch (BiffException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            // this is where i call for nested forloop
            Sheet s = wb.getSheet(0);
            int row = s.getRows();
            int col = s.getColumns();
            System.out.println("redovi" + row + "kolone" + col);

            for (int i = 0; i < 18; i++)
            {
                Stavka stavka = new Stavka(); // new instance <<<<<<<<

                for (int j = 0; j < col; j++)
                {
                    Cell c = s.getCell(j,i);
                    if(j==0) {stavka.setStavkaID(Integer.parseInt(c.getContents().toString()));}
                    else if(j==1) {}
                    else if(j==2) {stavka.setSifraKomponente(c.getContents().toString());}
                    else if(j==3) {stavka.setOpis(c.getContents().toString());}
                    else if(j==4) {stavka.setVrednost(c.getContents().toString());}
                    else if(j==5) {stavka.setKuciste(c.getContents().toString());}
                    else if(j==6) {stavka.setSektor(c.getContents().toString());}
                    else if(j==7) {stavka.setRack(c.getContents().toString());}
                    else if(j==8) {stavka.setProizvodjac(c.getContents().toString());}
                    else if(j==9) {stavka.setKolicina(Integer.parseInt(c.getContents().toString()));}

                }   
                lista.add(stavka); // inside second for loop <<<<<<<<
            }

            // outside the second for loop <<<<<<<<<<<<<<<<<<<<<<<<<<
            // this is my tableModel
            TabelaStavka stavka1 = new TabelaStavka(lista);
            tblAzuriranjeMagacina.setModel(stavka1);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM