如果Excel文件处于打开状态,Excel文件不能被Java-Program访问

我的代码应该打印一个数字(在这种情况下数字5)到指定的Excel工作簿中指定的单元格。 如果Excel-Workbookclosures,代码将起作用。 但是,如果Excel-Workbook打开,运行代码时出现以下错误消息:

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) 

代码如下:

 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(); 

我想在运行java代码时保持文件打开。 有人知道我必须改变,以便能够保持运行代码时打开Excel文件?

Microsoft Excel将它已打开的文件放在独占锁。

因此,如果在Excel中打开文件时尝试写入任何其他应用程序中的文件,则在Windows上将失败。

当您需要写入文件时,可能没有其他解决scheme然后closuresExcel。