![](/img/trans.png)
[英]How to add new excel sheet in existing excel file in runtime using java (jxl) in Webdriver
[英]How to add a row in an existing Excel file using Java?
如何使用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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.