我必须更新现有的Excel工作表以捕获结果。结果应该在特定行的excel表中添加,而不是按顺序添加。 这些值在ArrayList中提供,但最后一个索引仅在Excel工作表中添加。以下是代码:
public static ArrayList<String> writeResult(String filename, ArrayList<String> code, ArrayList<String> value) throws IOException, InvalidFormatException{
FileInputStream fis=new FileInputStream(filename);
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet("DataSheet-Execution");
String statusString="";
int count = 0;
Row row=sh.getRow(5);
for(int k=0;k<code.size();k++){
for(int j=5;j<=28;j++)
{
for(int i=0;i<=row.getLastCellNum();i++)
{
if(row.getCell(i) != null)
{
row = sh.getRow(j);
Cell status=row.getCell(4);
statusString = status.toString();
if(statusString.equals("Y"))
{
Cell cellxAction=row.createCell(26);
Cell cellRes=row.createCell(27);
cellxAction.setCellValue(code.get(k).toString());
cellRes.setCellValue(value.get(k).toString());
}
}
}
}
}
FileOutputStream fos=new FileOutputStream(filename);
wb.write(fos);
fos.close();
System.out.println("Excel File Written");
return value;
}