我需要将csv数据显示到jtable

j8ag8udp  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(158)

我可以读取csv文件,但当行为空或空白时,它会显示出来。

try {

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV", "csv");
        chooser.setFileFilter(filter);
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        filename=f.getAbsolutePath();
        lbl_Path.setText(filename);

       BufferedReader br = new BufferedReader(new FileReader(filename));
       String fristLine = br.readLine().trim();

       String[] columnIdentifiers = fristLine.split(",");   
       Object[] seleColmNm = {columnIdentifiers[0], columnIdentifiers[1], columnIdentifiers[3]};
       DefaultTableModel model = (DefaultTableModel)csv_Table.getModel(); 
       model.setColumnIdentifiers(seleColmNm);

       Object[] tableLines = br.lines().toArray();

            for (int i = 0; i < tableLines.length; i++)
            {
                String line = tableLines[i].toString().trim();
                String[] dataRow = line.split(",");   
                Object[] seleColm= {dataRow[0], dataRow[1], dataRow[3]};
                model.addRow(seleColm);
            }

         br.close();

    } catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }

csv数据
身份证|姓名|年龄|代码
1 |萨曼| 12 | 110
2 |(空)| 16 | 002
3 |阿里| 13 |(空)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题