FileNotFoundException

Hi,

I have a scenario to write data to an excel which contains around 5 sheets.I have written Custom Keyword for writing data to a sheet in excel passing sheet name as parameter to write to a particular sheet.

When writing to a particular sheet and writing in next step to another, i am facing FileNotFoundException. ( The process cannot access the file as it is used by another process.)

Could someone help me on this?

Thanks in advance!!!

Thanks,
Udaya Bhaskar

How are you opening the file?

Is the Excel Sheet opened by Excel or maybe there is a Excel Background process open, which blocks the writing to the excel sheet.

Hi Andreas_Voit,

The below is my CustomKeyword:

public class WriteExcel {
Workbook wbwrite;
String epc_Filepath=GlobalVariable.EPC_Input
String Filepath_write=GlobalVariable.Filepath_Input
@Keyword
public void writeData(String sheetName,String value,int row, int column){
File file1 = new File(Filepath_write)
InputStream wis=new FileInputStream(file1)
wbwrite= new XSSFWorkbook(wis)
XSSFSheet shw=wbwrite.getSheet(sheetName)
Cell cell=shw.getRow(row).getCell(column)
cell.setCellType(1)
cell.setCellValue(value)
OutputStream wos= new FileOutputStream(file1)
wbwrite.write(wos)
wos.flush()
wos.close()
wis.close()
}
}

The Excel file contains 3 sheets. The issue is occurring when writing to sheet2 and again in the next step trying to write to sheet1 and sometimes when writing to same sheets.

Thanks,
Udaya Bhaskar

I didn’t tested your example, but I think you should also close the file?

file.close();

I found an example:

@Keyword
def void writeToExcel(String text ){
	FileInputStream file = new FileInputStream (new File("D:\\Testdata.xlsx"))
	XSSFWorkbook workbook = new XSSFWorkbook(file);
	XSSFSheet sheet = workbook.getSheetAt(0);
	Cell searchText = sheet.getRow(1).getCell(1);
	searchText.setCellValue(text);
	file.close();
	FileOutputStream outFile =new FileOutputStream(new File("D:\\Testdata.xlsx"));
	workbook.write(outFile);
	outFile.close();
}

Source: Write into excel at runtime during test execution - #6 by nhi

Theres also an example for your usecase:

Tips and tricks is your friend :slight_smile: