Closing a ClosedXML generated Workbook.

Apr 25, 2014 at 11:59 AM
Good morning,

I have the following code:
Private Sub Btn_export_Click() Handles Btn_export.Click
        Dim book As XLWorkbook = New XLWorkbook(XLEventTracking.Disabled)
        Dim table As System.Data.DataTable

        table = DGV_preview.DataSource
        book.AddWorksheet(table, "Result")
    End Sub
Basically I want to create an Excel file, transfer a DataTable into it, Save the file, e-mail it and then delete the file.
Everything works perfectly until I want to delete the file. How on earth do I close the file so I can delete it? I can't seem to find the way to do it. :(

Thank you VERY much for this amazing work you've done, by the way.
Apr 25, 2014 at 6:19 PM
It should work as is because ClosedXML only locks the file when it reads or writes to it.

First try getting rid of all those .Dispose(), they're not doing anything in your case.

If that doesn't work you could try putting the workbook/file creation and the file delete in two different functions, then call both from the click event.
Apr 25, 2014 at 6:24 PM
It is also possible that the email sending is not releasing the file.
Marked as answer by JSG85 on 4/28/2014 at 1:24 AM
Apr 28, 2014 at 8:24 AM
Good morning to both of you,

Apologies for not having replied sooner, but I couldn't get ahold of the code to test it until today.
As roberttanenbaum said, it was the email sending process not releasing the file. I commented the whole block where the mail was being sent and the File.Delete line worked like a charm.

Thanks for both of your replies, and especially to the ClosedXML crew for making such a wonderful piece of code.

Good day!