How to force Close

Mar 22, 2011 at 4:55 PM

I really like this library except for one thing - the inability to close the spreadsheet when finished.  Saving does save the information, but the file remains open and can't be used until you totally exit the program.  What I'm trying to do is create the spreadsheet from a dataset using ClosedXML and then open the spreadsheet to allow the user to make changes or save it where they wish.  The creation part works fine, but I cannot open it after.

As an example, we have one case where a user populates a spreadsheet and then needs to add data from our main database.  After that, there is more manipulation of the data before it is sent to another office.  Ideally the user would select the spreadsheet, the program would get the data from the database and add it to the spreadsheet, save it and then the saved spreadsheet would open so the user could continue working.   I could do this if only I could close the spreadsheet when I was finished without closing the entire program.

I've been using EPPPlus, but ClosedXML seems to be a much cleaner and faster library.  If only I could close the spreadsheets when finished.  Is this possible?

Mar 22, 2011 at 5:07 PM

I should have made clear that I'm using a Windows Forms application, not a web app.

Mar 22, 2011 at 5:08 PM

Someone reported the same issue before but I wasn't able to reproduce the error. Check out this discussion

Please try the following and let me know if you're able to open your file with ClosedXML and then with Excel (while still running the program):

       static void Main(string[] args)
            var wb = new XLWorkbook(@"AnyExcelFile.xlsx");
            Console.WriteLine("Try to open the file with Excel and press any key to end.");

Mar 22, 2011 at 6:04 PM

Problem solved.  Thanks for your help.  I needed to set a flag in the file open command to allow it to open even if another process already had it open.  But I still consider that just a work around.  It would be better to have an option in ClosedXML to close the spreadsheet when finished.  Consider it as part of a larger system.  When each module completes its work it closes all files and passes processing to the next module.  Right now you can't do that in ClosedXML without physically closing the program.

Mar 22, 2011 at 6:16 PM

Here's where you lose me. Once the file is read, ClosedXML doesn't hold any lock on the file. In the example above you don't have to close the program in order to open/save it with Excel. Could you please give an example where you have to close the program to use the file with another program like MS Excel?


Mar 23, 2011 at 2:26 PM

OK, it was a problem in my COM automation code.  Fixed now.  So you are correct, I don't need a close method.  Thanks!