3

I'm new on the website. I try to write to an excel file while it is opened (using POI / JAVA).

I got a java.io.FileNotFoundException error when i attempt to create a file output stream to write to the file.

FileOutputStream didn't work. I receive this following message:

the process cannot access the file because it is being used by another process.

         try {
           FileOutputStream fileOut;
           XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("Classeur.xlsm")); 
           ...
           ...
           fileOut = new FileOutputStream("Classeur.xlsm");
           wb.write(fileOut);
           fileOut.close();
           }
        catch (FileNotFoundException e){
           e.printStackTrace();
           } 
        catch (IOException e) {
           e.printStackTrace();
           }

Does anybody know how i can fix it? i'm writting an (POI-Java-Swing) application to dynamically read/write in excel files.

Thanks for your help

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Jo Perone
  • 81
  • 1
  • 5

5 Answers5

4

Windows itself will not let you do this. Nothing in your code will let you do so, I'm afraid. You need to close the file in Excel first.

Edit - I am assuming from your description that you've got the file open yourself, in Excel. You might not be. However something else certainly does - it could be another instance of your Java application, for example. Even make sure stuff like Windows Explorer isn't accessing/using the file, e.g. to check attributes. And until you rule that out, as mentioned Windows itself will block you accessing it.

Brian
  • 6,391
  • 3
  • 33
  • 49
  • effectively, I opened the file. This is important because it will enable me to access updated data dynamically and displayed it in swing. And also for using excel macro. The file is too large to be opened / closed whenever the user wants to modify data (Java Swing Poi-Excel) or when the results are displayed (Excel-Poi-Java-Swing). Is it possible to unlock the excel file for access from Java? – Jo Perone Oct 17 '12 at 16:54
  • Afraid not, it sounds like you might be best looking at some sort of intermediary resource? E.g. an MS Access DB file which both Excel can read from, and you can write to? – Brian Oct 18 '12 at 08:27
  • I don't really know access. My excel file is very complex with some macro. – Jo Perone Oct 18 '12 at 15:22
3

There is another process running somewhere that is accessing the exact Excel file you're trying to write to. Either see if there are no Excel applications (like Microsoft Excel) running that have a lock to your file or a Java process that has already got a lock to the same file.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • i opened the file. So i suppose that is MS Excel. The file is too heavy to be open/close each time I want to edit data and display the results(updated). – Jo Perone Oct 17 '12 at 16:59
1

If the file is open, you will get java.io.FileNotFoundException. That's why, before write the file you have to check it.

Example :

File file = new File("workbook.xls");
if(file.canWrite()) {
    System.out.println("File is open");
} else {
    FileOutputStream fileOut = new FileOutputStream(file);
    wb.write(fileOut);
    fileOut.close();
}
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
0

A similar problem discussed in the I can't delete a file in java topic. Maybe it helps you to find a right way to solve your problem.

Community
  • 1
  • 1
Dmytro Chyzhykov
  • 1,814
  • 1
  • 20
  • 17
  • thanks for the link but I do not think it solves the problem. – Jo Perone Oct 17 '12 at 17:05
  • guys from that topic tried to null Stream reference and invoked `System.gc();` to suggest JVM expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. But that's just a hint. – Dmytro Chyzhykov Oct 17 '12 at 17:43
0

Well I don't Know whether this will work but I guess you should give it a try.

When I was doing a similar thing as you are doing I learnt that excel formulas can refer to cells of another excel files like for example

='D:\excelfiles\[Book1.xlsx]Sheet1'!$A$1

So why don't you update another file like Book1.xlsx ( which is closed so you won't get Exception) and let your opened file refer to the location of cells in Book1.xlsx

Well I am expecting a lot of downvotes here but I think its worth a try :)

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40