My code is supposed to print a number (in this case the number 5) to a specified cell in a specified Excel Workbook. The code works if the Excel-Workbook is closed. However, if the Excel-Workbook is open, I get the following error message when running the code:
Exception in thread "main" java.io.FileNotFoundException: C:\Users\Username\Desktop\java-Programs\test.xlsm (The process can't access the file because it is used by another process)
The code is the following:
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\Username\\Desktop\\java-Programs\\test.xlsm")));
XSSFSheet ExcelSheet = wb.getSheet("Output");
XSSFRow row = ExcelSheet.getRow(3);
XSSFCell cell = row.getCell(0);
cell.setCellValue(5);
FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Username\\Desktop\\java-Programs\\test.xlsm");
wb.write(fileOut);
fileOut.close();
I want to keep the file open when running the java code. Does anybody know what I have to change in order to be able to keep the Excel-File open when running the code ?