We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Workbook.save api delete file and create new file

Hi Team,


I am facing one issue related to workbook.save(“C:\ExcelFile.xlsx”) api.

I am creating a workbook object by giving same file path which I have specified above. I have added some data in sheets. Now when I call workbook.save then some times I have observed that file gets deleted.

Is aspose delete and create new file at the given location? If yes what should we use if we don’t want to delete and create new file? We want it to update same file at a given location.

Thanks & Regards,
Kapil Jaiswal

Hi,

Thanks for your posting and using Aspose.Cells.

No, your file is not deleted but it is overwritten. If you want that it is not overwritten then please change the name of your output file that should be different than your source excel file.

Suppose your source excel file is

F:\temp\abc.xlsx

then you can save it as

F:\temp\abc-output.xlsx

Now a new file abc-output.xlsx will be created and your existing abc.xlsx file will not be changed.

Hi Team,

Thanks for a quick reply. What should I do if I want to modify same file?

Thanks & Regards,
Kapil Jaiswal

Hi Kapil,


If you wish to modify an existing file then you should save the resultant file with same location & file name that was used to load the original spreadsheet. For instance, if your input spreadsheet’s name is abc.xlsx and it resides on the root of C drive then you would be loading it as follow.

Java

Workbook workbook = new Workbook(“C:/abc.xlsx”);

Once you have made the necessary changes to the spreadsheet, and you wish to overwrite the original file, you should save it as follow.

Java

workbook.save("C:/abc.xlsx");

Overwrite means it replaces an existing file, correct?

If that is the case then I don’t want to replace an existing file.
In aspose documentation I have seen use of FileOutputStream,

FileOutputStream stream=new FileOutputStream(“C:\book1.xls”);
//Save in Excel2003 format
workbook.save(stream, SaveFormat.EXCEL_97_TO_2003);

Will this API write into same file or Overwrite that file?

Performance wise, is there any difference in these two flavor of save API’s?

Hi,

Thanks for your question and using Aspose.Cells.

Actually workbook object is created with your source excel file and after that it has nothing to do with your source excel file. Everything is done inside the workbook object and nothing happens with your source excel file with which your workbook object is constructed. It means everything is done inside the memory not hard drive.

So when you want to save your workbook object in disk, you provide it some name, if the name is same as your source excel file, then your existing source excel file is overwritten, means the existing source excel file is lost and the new excel file with the same name is generated.

Hi Shakeel,


Thanks for the information. As you have stated, that aspose perform all operations in memory and at the time of saving a file with same name, first it removes the existing file and then create new file with same name.
So this is the area where some times I end up with file not found.

This may be the case with large data files, like at my end file contains data with more than 1 lakh rows with 220 columns for each row. So as per my understanding, while saving workbook if aspose got any error in between, then as aspose has already removed that file, so I will not get that file again. If I want to perform other operations I end up with no file at that location.

Can aspose not use other way round?
i.e. first create a file with in memory workbook data with any name and when file is created successfully then remove the file on disk and rename the newly created file.

Please tell me if my understanding goes wrong?

Thanks & Regards,
Kapil Jaiswal

Hi Kapil,

Thanks for your nice inquisitive questions and considering Aspose.Cells.

Actually, you do not get any performance improvement if you use the same file name on saving. Because Aspose.Cells will load all the data inside the memory and when you save it back with the same file name, then that file stream is first deleted and then overwritten. The file actually does not get deleted but the data inside it is deleted. This has nothing to do with Aspose.Cells. It is actually the behavior of Java and .NET file stream APIs. You can replicate it by first loading your file in file stream and then writing it again on the same file stream, then existing stream will be deleted and new stream will be written.

What I understand, you want to do it because of performance and memory optimization but this way you are not getting any performance improvement but also your main file is overwritten too. So I suggest you use different file name on re-saving.

Thanks Shakeel for reply, that clears my doubts.


One more query, do aspose have any API which tells me the file is opened by some other user before calling save API of workbook?

I am asking this question because if file is opened in MS Excel and at that time if I try to call save API that time it throws exception, so before calling save API I need to know weather file is in open state.

Thanks & Regards,
Kapil Jaiswal

Hi Kapil,

Thanks for your question and using Aspose.Cells.

You can find it through some Java APIs but for that you need to research on internet.

Here is the one such query on StackOverflow related to your question.

( http://stackoverflow.com/questions/9341505/how-to-check-if-a-file-is-open-by-another-process-java-linux )