You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Suladna <su...@yahoo.com> on 2009/04/20 13:20:31 UTC

Unable to save Excel file after having connected it to Java

Hi!
 
I'm using the below code to bring text from Excel to Java:
 
XSSFWorkbook wb = new XSSFWorkbook("C:/myDocument.xlsm"); 
XSSFSheet sheet = wb.getSheet("Sheet1");
Row firstRow = sheet.getRow(0);
System.out.println(firstRow.getCell(0).getRichStringCellValue().getString())
 
If I run the above program and terminate it, I am able to save myDocument.xlsm. However, if I run the program without terminating it (i.e. I let it continue running other code), I am not able to save myDocument.xlsm!
 
If I try doing so, I get an error message telling me that the document can't be saved because of a sharing error and that I should try saving it to another file. Can anything be done to prevent this error?
 
Best regards,
Sul Adna


      

Re: Unable to save Excel file after having connected it to Java

Posted by MSB <ma...@tiscali.co.uk>.
By no means should I be considered an expert with XSSF - in fact I have only
used this stream once or twice as I do not have Office 2007 on my machine.
Nevertheless, I believe that somewhere along the line, the XSSFWorkbook
class uses a stream to connect to the spreadsheet file in order to read it's
contents. It is likely that the stream is the object that is causing your
file lock. So, if it is possible, pass something like a FileInputStream
object to the XSSFWorkbook constructor. As you have access to the stream
object in your code, all you need to do is call the close method on it to
release the lock on the file; something like this.

FileInputStream fileIStream = new FileInputStream(new
File("................."));
XSSFWorkbook workbook = new XSSFWorkbook(fileIStream);
.........

then when you want to release the file, just call;

fileIStream.close();


Suladna wrote:
> 
> Hi!
>  
> I'm using the below code to bring text from Excel to Java:
>  
> XSSFWorkbook wb = new XSSFWorkbook("C:/myDocument.xlsm"); 
> XSSFSheet sheet = wb.getSheet("Sheet1");
> Row firstRow = sheet.getRow(0);
> System.out.println(firstRow.getCell(0).getRichStringCellValue().getString())
>  
> If I run the above program and terminate it, I am able to save
> myDocument.xlsm. However, if I run the program without terminating it
> (i.e. I let it continue running other code), I am not able to save
> myDocument.xlsm!
>  
> If I try doing so, I get an error message telling me that the document
> can't be saved because of a sharing error and that I should try saving it
> to another file. Can anything be done to prevent this error?
>  
> Best regards,
> Sul Adna
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Unable-to-save-Excel-file-after-having-connected-it-to-Java-tp23134819p23149963.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Unable to save Excel file after having connected it to Java

Posted by Suladna <su...@yahoo.com>.
I'm afraid that alternative 1 is not possible for me.. so how do I close the file in Java?
 
(I don't want to save it from Java, just close it so that I can save it from Excel, since changes can have been made in Excel without the Java program knowing it).
 
/Sul


--- On Mon, 4/20/09, Nick Burch <ni...@torchbox.com> wrote:

From: Nick Burch <ni...@torchbox.com>
Subject: Re: Unable to save Excel file after having connected it to Java
To: "POI Users List" <us...@poi.apache.org>, suladna@yahoo.com
Date: Monday, April 20, 2009, 11:43 AM

On Mon, 20 Apr 2009, Suladna wrote:
> If I try doing so, I get an error message telling me that the document
can't be saved because of a sharing error and that I should try saving it to
another file. Can anything be done to prevent this error?

Ah, the joys of windows and opening files. Two options:
1) use unix
2) close the file in java before trying to save it in excel

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org




      

Re: Unable to save Excel file after having connected it to Java

Posted by Nick Burch <ni...@torchbox.com>.
On Mon, 20 Apr 2009, Suladna wrote:
> If I try doing so, I get an error message telling me that the document 
> can't be saved because of a sharing error and that I should try saving 
> it to another file. Can anything be done to prevent this error?

Ah, the joys of windows and opening files. Two options:
1) use unix
2) close the file in java before trying to save it in excel

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org