如何使用Java在现有的Excel文件中添加一行?

如何使用Java在现有的Excel文件中添加一行? 我试过使用这个代码,但它不工作。 请告诉我需要做什么更正。

try { String s1 ,s2,s3,s4; s1 = jTextField1.getText(); s2 = jTextField2.getText(); s3 = jTextField3.getText(); s4 = jTextField4.getText(); FileInputStream fis = null; File excel = new File ("E:\\two\\record.xlsx"); fis = new FileInputStream(excel); XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet ws = wb.getSheet("cmss"); int rowNum = ws.getLastRowNum() + 1; Row row = ws.createRow(4); String[] s = new String[4]; s[0] = s1; s[1] = s2; s[2] = s3; s[3] = s4; for (int i = 0; i < 4; i++) { Cell cell = row.createCell(i); cell.setCellValue(s[i]); } jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField4.setText(""); } catch (FileNotFoundException ex) { Logger.getLogger(Addxl.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Addxl.class.getName()).log(Level.SEVERE, null, ex); } 

你添加行后写出了文件吗? 例如:

 wb.write(new FileOutputStream(excel));