-1

I am trying to save and close an existing workbook that I am already successfully opening but for some reason cannot either save and close:

//declarations etc here...

try {
            InputStream ExcelFileToRead = new FileInputStream(file);
            XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);


            //XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(file)));
            XSSFSheet sheet = wb.getSheetAt(0);
            XSSFRow row;
            XSSFCell cell;
            int rows; 
            rows = sheet.getPhysicalNumberOfRows();
            int cols = 1; 
            XSSFRichTextString path;
            String stpath;


            try {
            if(!Desktop.isDesktopSupported()){
                    System.out.println("Error: Desktop is not supported");
            }
            Desktop desktop = Desktop.getDesktop();
            if(filee.exists()) desktop.open(filee);
            FileOutputStream out = new FileOutputStream(file);  
            wb.write(out);
            out.close();

//code continues...

wb.write(out) opens the file successfully. I have read tons of posts/articles/docs all using that close() method to close out an XSSF Excel file but it does not work here.

How do I solve the problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
Elvis
  • 21
  • 5
  • How do you it know it "does not work", and what does "does not work" mean? `//declarations etc here...` why do you think these are not necessary to understand your problem? For example, what are `file` and `filee`? Please visit the [help], take the [tour] and especially read [ask] to learn how to use this site effectively. – Jim Garrison Aug 11 '19 at 00:03
  • Hi sorry about that, anyway the guy below got what the issue is right to the point so the code so quite enough. Anyway just for further clarification, filee and file are the same path of the same Excel file. Just filee is a file object whereas file is a string. I tried to use both to solve the issue but the problem is that I can only save and close with a path different from the one of the existing file, as perfectly described by Jim Garrison below. – Elvis Aug 11 '19 at 08:55

1 Answers1

0

You cannot overwrite the input document this way, as clearly documented in the Javadoc for POIXMLDocument#write(OutputStream o) which is inherited by XSSFWorkBook:

Note - if the Document was opened from a File rather than an InputStream, you must write out to a different file, overwriting via an OutputStream isn't possible

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • You are right I just figured out that I could save it with a different file name. But that is not what I want. I basically need to just open, save, close and read an xlsx file. The only reason why I want to open save and close (rather than just reading it) is because the data I need to read are formulas so I get the last value that were saved. E.g. If I have a formula like today() which gives today date and I save the Excel today, tomorrow Java will still get the yesterday date rather than the new today date. Any advice? – Elvis Aug 11 '19 at 08:59