将Excel信息添加到JTable,而不是创build新的行? (.xlsx)(JAVA)

我正在使用Apache POI,并试图从Excel工作簿中获取信息(了解这是一个.xlsx文件,我使用POI中的XSSF),并将其显示到一个JTable上。 但是,我看到这不是创造新的专栏。 我已经注意到,当我将它存储到新的向量中,创build一个向量时,我认为它并没有为它遍历的每一行创build一个新的向量。

我将如何存储这些信息? 我想我将不得不存储vector,datatemp,然后清除它,每次find一个新的行,并将其添加到数据向量。 但是,我将如何做到这一点? 我在正确的轨道上?

图片:(不知道我是否被允许发布这个由于没有10代表,但如果不是,请删除)

http://i.stack.imgur.com/AXg0v.png

http://i.stack.imgur.com/RuId5.png

package table_testing_broken_up; import java.awt.BorderLayout; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class pull_information extends JFrame { private static Vector dataTemp = new Vector(); private static Vector columns = new Vector(); private static Vector data = new Vector(); public static DefaultTableModel model = null; public static JTable table = new JTable(); public static void main(String[] args) { makeFrame(); } static void makeFrame() { setData(); JFrame frame = new JFrame("Table Test"); frame.setSize(500, 150); data.addElement(dataTemp); model = new DefaultTableModel(data, columns); table.setModel(model); table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS); frame.add(table.getTableHeader(), BorderLayout.PAGE_START); frame.add(table); frame.setVisible(true); } static void setData() { try { FileInputStream file = new FileInputStream( new File("C:\\Testing\\student_employment_form.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); if(row.getRowNum() == 0) { while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); columns.add(cell.getStringCellValue()); if(row.getRowNum() == 1) { break; } } continue; } while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); if(rowIterator.hasNext()) { if(cell.getCellType() == Cell.CELL_TYPE_STRING) { dataTemp.add(cell.getStringCellValue()); } else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { dataTemp.add(cell.getNumericCellValue()); } //data.addElement(dataTemp); } else { break; } } } file.close(); } catch (FileNotFoundException e) { System.out.println("File Not found"); } catch(IOException e) { e.printStackTrace(); } } } 

其他研究:

我知道我可以使用:JTableReadTableModelTask​​,但是,我将不得不使用JComponentPack v3.5这似乎不是开源,并花费金钱。

注:这是为了工作,只是我正在努力使我的工作更快的一个小项目,我是一个在科学与math科学学士学位追求的本科生。 另外这只是一个testing文件,我将这个实现到我的主程序,并推到git。

任何build议都会被大大的附着! 谢谢!

我想我将不得不存储vector,datatemp,然后清除它,每次find一个新的行,并将其添加到数据向量

不,你不能清除它,因为vector被添加到TableModel,所以如果你清除它,每一行将有一个引用相同的向量。

我在正确的轨道上?

是的,但是您需要为每一行数据创build一个新的Vector,以便每行可以引用包含列数据的不同Vector。 就像是:

 dataTemp = new Vector(); // added while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); if(rowIterator.hasNext()) { if(cell.getCellType() == Cell.CELL_TYPE_STRING) { dataTemp.add(cell.getStringCellValue()); } else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { dataTemp.add(cell.getNumericCellValue()); } //data.addElement(dataTemp); } else { break; } } data.add( dataTemp ); // added 

现在你的datavector是vectorvector,它代表每一行的向量,每行vector有一个vector或行的每一列。

编辑:

 Iterator<Row> rowIterator = sheet.iterator(); while(rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); if(row.getRowNum() == 0) { while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); columns.add(cell.getStringCellValue()); } } else { dataTemp = new Vector(); while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); if(cell.getCellType() == Cell.CELL_TYPE_STRING) { dataTemp.add(cell.getStringCellValue()); } else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { dataTemp.add(cell.getNumericCellValue()); } } data.add( dataTemp ); } 

}