【问题标题】:how to read xls file into jtable如何将xls文件读入jtable
【发布时间】:2018-08-02 08:00:54
【问题描述】:

我无法将 XLS 数据导入 jtable。

我的程序只读取 XLS 的最后一行。

这是我的代码:

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);
            }
        }
    }
}

如何解决这个问题?

【问题讨论】:

    标签: java excel import jtable


    【解决方案1】:

    我认为存在错误,因为您必须为 XLS 中的每一行创建一个新的 Stavka 对象,然后将其添加到“lista”。最后,我相信您还想在这些for 循环之外设置模型(TabelaStavka)。

    我这里没有安装完整的Java环境,如果下面有任何语法错误请见谅。最重要的是您了解您必须在代码中进行哪些修复。

    此外,永远不要低估代码格式,您应该考虑使用更好的 IDE,这有助于更好地格式化您的源代码。

    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);
            }
        }
    }
    

    【讨论】:

    • 感谢您的帮助。但主要问题是我在 64000 nd 单元格上有一些东西来自 xls 的东西,它在那里坏了
    猜你喜欢
    • 1970-01-01
    • 2012-09-22
    • 2023-03-07
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 2015-04-29
    • 1970-01-01
    相关资源
    最近更新 更多