如何使用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);
}
答案 0 :(得分:3)
添加行后是否写出了文件?例如:
wb.write(new FileOutputStream(excel));