如何从Excel文件填充JTable,只要我的数组列表中有多个匹配的元素? 【JAVA]

[![1]] [1]我有一个从Excel文件填充JTable的问题。 这里是操作,我会search,让我们说“第1行”,有2个单元格匹配这个值,如果有匹配,我想拉行,并插入到我的JTable。 我能够得到这个工作,但是,这只会创build一行的第一个匹配的值,当我再次点击searchbutton,它将取代行,用一个新的行,如果有超过1匹配。

我会让jtable在同一个表中添加两行,而不是一个一个地添加。 我附上了迄今为止我所拥有的。

先进的谢谢你。

try { FileInputStream fis = new FileInputStream( new File("S:\\Tester Support\\0LineTester Database\\Audit\\LASAudit.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { if (cell.getRichStringCellValue().getString().trim().equals(LinNum_Text.getText())) { int rowNum = row.getRowNum(); Row r = sheet.getRow(rowNum); DefaultTableModel model = new DefaultTableModel(); int col_0 = 0; String val_0 = r.getCell(col_0).getStringCellValue(); int col_1 = 1; String val_1 = r.getCell(col_1).getStringCellValue(); int col_2 = 2; String val_2 = r.getCell(col_2).getStringCellValue(); int col_3 = 3; String val_3 = r.getCell(col_3).getStringCellValue(); model.addColumn("ID", new Object[] { val_0 }); model.addColumn("Date", new Object[] { val_1 }); model.addColumn("Auditor Name", new Object[] { val_2 }); model.addColumn("Line #", new Object[] { val_3 }); table = new JTable(model); table.setVisible(true); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 278, 670, 303); contentPane.add(scrollPane); scrollPane.setViewportView(table); } } } } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } 

我能够得到这个工作,但是,这只为第一个匹配值创build一行,

这是因为在你的“循环代码”中你每次都创build一个新的JTable。

相反,您只需创build一次表格,并将数据添加到循环内的TableModel中。 所以你的代码的结构应该是这样的:

 String[] columnNames = { "Id", "Date", ... }; DefaultTableModel model = new DefaultTableModel(columnNames, 0); for (...) { ... if (matched row found) { ... Object[] row = { val_0, val_1, ...}; model.addRow( row ); } JTable table = new JTable( model ); ... 

随着camickr的帮助,我得到了这个工作。 这是最后的代码。

 try { FileInputStream fis = new FileInputStream( new File("S:\\Tester Support\\0LineTester Database\\Audit\\LASAudit.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); String[] columnNames = {"Id","Date","Auditor Name","Line Number"}; DefaultTableModel model = new DefaultTableModel(columnNames, 0); JTable table = new JTable(model); table.setVisible(true); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 278, 670, 303); contentPane.add(scrollPane); scrollPane.setViewportView(table); for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { if (cell.getRichStringCellValue().getString().trim().equals(Spec_Text.getText())) { int rowNum = row.getRowNum(); Row r = sheet.getRow(rowNum); int col_0 = 0; String val_0 = r.getCell(col_0).getStringCellValue(); int col_1 = 1; String val_1 = r.getCell(col_1).getStringCellValue(); int col_2 = 2; String val_2 = r.getCell(col_2).getStringCellValue(); int col_3 = 3; String val_3 = r.getCell(col_3).getStringCellValue(); Object[] ROWS = { val_0, val_1, val_2,val_3}; model.addRow( ROWS ); } } } } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } }